2017-03-02 2 views
0

저는 Zend Framework 1을 3 년 사용한 후에 Zend Framework 3을 사용하기로 결정했습니다.이 결정으로 인해 일이 더 쉬워지는 대신 Zend 3이 두통을 겪었습니다.모델 또는 도우미의 기본 경로를 얻는 방법 Zend Framework 3

public function getUrl(string $file = '') 
{ 
    if($this->_helperBaseUrl === null) { 
     $this->_helperBaseUrl = new Zend_View_Helper_BaseUrl(); 
    } 
    return $this->_helperBaseUrl->baseUrl($file); 
} 

public function getSkinUrl(string $file = '') 
{ 
    $themePath = 'themes/my-theme/'; //get from database 
    return $this->getUrl($themePath . ltrim($file, '/\\')); 
} 

그런 다음 응용 프로그램의 어떤 부분에서 (모델, 헬퍼, 플러그인 및 뷰) 나는이 기능에 액세스 할 수 있습니다 다음과 같이

젠드 1, 나는 데이터베이스에서 선택한 템플릿에 대한 URL을 사용자 정의 like :

//view/scripts/index/index.phtml 
$url_logo = My::app()->getSkinUrl('logo.jpg'); 
//this return http://example.com/themes/my-theme/logo.jpg 

Zend 3에서는 매우 어려웠습니다. 누구든지 Zend 3에서 어떤 방법을 사용하는지 알고 있습니까? 또는 Zend 3의 모델에서 baseUrl을 얻는 방법?

+0

당신 늦게 파티에 오년 : – tasmaniski

+0

@ tasmaniski 3 년 전 ZF2를 사용했지만 ZF1보다 느렸다. 그 이유로 저는 ZF1을 계속 사용했습니다. :) –

답변

2

젠드 프레임 워크 2/3에서는 거의 모든 클래스를 다른 클래스에 삽입 할 수 있습니다. 예를 들어 basePath 플러그인 (보기 컨텍스트에서 사용 가능)이 필요한 경우이 플러그인을 모델/서비스 또는 컨트롤러 클래스에 삽입 할 수 있습니다. 이것은 권장되는 방법입니다

이이 플러그인 또는

use Zend\View\Helper\BasePath; 

class MyService 
{ 
    /** 
    * @var BasePath 
    */ 
    protected $plugin; 

    /** 
    * MyService constructor. 
    * 
    * @param BasePath $basePath 
    */ 
    public function __construct(BasePath $basePath) 
    { 
     $this->plugin = $basePath; 
    } 

    /** 
    * @return BasePath 
    */ 
    public function getPlugin() 
    { 
     return $this->plugin; 
    } 

    /** 
    * @param BasePath $plugin 
    */ 
    public function setPlugin($plugin) 
    { 
     $this->plugin = $plugin; 
    } 
} 

지금, 다른

use Interop\Container\ContainerInterface; 
use Zend\ServiceManager\FactoryInterface; 
use Zend\ServiceManager\ServiceLocatorInterface; 
use MyNamespace\Service\MyService; 

class MyServiceFactory implements FactoryInterface 
{ 
    /** 
    * 
    * @param ContainerInterface $container 
    * @param string $requestedName 
    * @param null|array $options 
    * @return MyService 
    */ 
    public function __invoke(ContainerInterface $container, $requestedName, array $options = null) 
    { 
     $class = $requestedName ? $requestedName : MyService::class; 
     $plugin = $container->get('ViewHelperManager')->get('BasePath'); // inject this class 
     $myService = new $class($plugin); // into this class 

     return $myService; 

    } 
    /** 
    * Provided for backwards compatibility; proxies to __invoke(). 
    * 
    * @param ContainerInterface|ServiceLocatorInterface $container 
    * @return MyService 
    */ 
    public function createService(ServiceLocatorInterface $container) 
    { 
     return $this($container, MyService::class); 
    } 
} 

확인에 하나 명의 의존성을 주입하기 위해 공장에 필요한 다른 서비스를 필요로 클래스, 현재 MyService에는 basePath 플러그인이 있지만 컨트롤러에 사용하려면 컨트롤러에 서비스를 주입해야합니다. 그래서 ...

인 IndexController

use MyNamespace\Service\MyService; 
use Zend\Mvc\Controller\AbstractActionController; 

class IndexController extends AbstractActionController 
{ 
    /** 
    * @var MyService 
    */ 
    protected $service; 

    /** 
    * IndexController constructor. 
    * 
    * @param MyService $service 
    */ 
    public function __construct(MyService $service) 
    { 
     $this->service = $service; 
    } 

    public function indexAction() 
    { 
     $plugin = $this->service->getPlugin(); // Zend\View\Helper\BasePath object 
     //... 
    } 
} 

... 그리고 우리의 컨트롤러 공장 ...

use Interop\Container\ContainerInterface; 
use Zend\ServiceManager\FactoryInterface; 
use Zend\ServiceManager\ServiceLocatorInterface; 
use MyNamespace\Controller\IndexController; 

class IndexControllerFactory implements FactoryInterface 
{ 
    /** 
    * 
    * @param ContainerInterface $container 
    * @param string $requestedName 
    * @param null|array $options 
    * @return IndexController 
    */ 
    public function __invoke(ContainerInterface $container, $requestedName, array $options = null) 
    { 
     $class = $requestedName ? $requestedName : IndexController::class; 
     $myService = $container->getServiceLocator()->get('MyNamespace\Service\MyService'); 
     $controller = new $class($myService); 

     return $controller; 

    } 
    /** 
    * Provided for backwards compatibility; proxies to __invoke(). 
    * 
    * @param ContainerInterface|ServiceLocatorInterface $container 
    * @return IndexController 
    */ 
    public function createService(ServiceLocatorInterface $container) 
    { 
     return $this($container, IndexController::class); 
    } 
} 

그것은 거의 이루어집니다. 마지막 단계는이 module.config.php 파일

use MyNamespace\Controller; 
use MyNamespace\Factory; 

return [ 
    //... 
    'service_manager' => [ 
     'factories' => [ 
      Service\MyService::class => Factory\Service\MyServiceFactory::class 
     ] 
    ], 
    'controllers' => [ 
     'factories' => [ 
      Controller\IndexController::class => Factory\Controller\IndexControllerFactory::class 
     ], 
    ], 
] 

쉬운 구성을 설정하는가요?
당신이 컨트롤러 플러그인,하지만 모델/서비스 클래스에서,이 "튜토리얼"의 MyService 부분을 건너 뛸 수 있습니다 및 컨트롤러 클래스에 직접 플러그인 주입이 필요한 경우

관련 문제