2017-03-02 4 views
0

私は最近、Zend Framework 1を3年間使用した後、Zend Framework 3を使用することに決めました。次のようにZendの1でモデルやヘルパーのベースパスを取得する方法Zend Framework 3

、私は、データベース内の選択したテンプレートのURLをカスタマイズ:

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, '/\\')); 
} 

次に、アプリケーション(モデル、ヘルパー、プラグインやビュー)のいずれかの部分で、私はこの機能にアクセスすることができます

//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

あなただけ遅れて相手に5年:) – tasmaniski

+0

@ tasmaniski 3年前私はZF2を使っていましたが、ZF1よりも遅かったです。その理由から、私はZF1を続けて使用しています:) –

答えて

2

Zend Framework 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; 
    } 
} 

を必要とするクラスで、[OK]を別の

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); 
    } 
} 

に1つの依存関係を注入する工場に必要な、今度はMyServicebasePathというプラグインがありますが、コントローラーで使用するにはサービスをコントローラーに注入する必要があります。だから... ...

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部分をスキップすることができますし、コントローラクラスに直接プラグインジェクトが必要な場合

関連する問題