2017-04-17 92 views
0

我正在使用它來構建zend應用程序。 http://github.com/zendframework/ZendSkeletonApplicationZend框架 - 獲取控制器內的配置

我試圖讓我把config/autoload/global.phpconfig/local.php.dist與底線裏面的配置數據,但它返回

Zend\ServiceManager\Exception\ServiceNotFoundException

A plugin by the name "getServiceLocator" was not found in the plugin manager Zend\Mvc\Controller\PluginManager

任何想法如何,我可以得到配置?

$config = $this->getServiceLocator()->get('config'); 

回答

0

爲了做到這一點,你需要注入的配置,因爲getServiceLocator(和所有其他定位器)已經從ZF3刪除。

在你的模塊配置你有這樣的:

'controllers' => [ 
    'factories' => [ 
     Controller\IndexController::class => InvokableFactory::class, 
    ], 
], 

你可以改變工廠創建你自己的。

Controller\IndexController::class => Controller\IndexControllerFactory::class, 

下面的代碼:

final class IndexControllerFactory 
{ 
    public function __invoke(Container $container) : IndexController 
    { 
     $config = $container->get('config'); 
     if (!isset($config['stuff']['stuff']) { 
      throw new \Exception('Please add the stuff.stuff parameter in the config'); 
     } 
     $myParam = $config['stuff']['stuff']; 
     return new IndexController($myParam); 
    } 
} 

Container是PSR容器。

在你的控制器中添加一個構造函數接收你所需要的配置:

public function __construct(string $param) 
{ 
    $this->param = $param; 
} 

在這裏,你必須在你的類的配置,作爲一個屬性。

+0

嗨,我有以下,但有錯誤: – sparkmix

0

在控制器使用Zend Framework 3.與getServiceLocator()的時刻ZendSkeletonApplication主分支已經remove in Zend Framework 3. 所以,如果你想從服務傳遞一些變量控制器,你應該建立一個工廠。在工廠實例化控制器時傳遞變量。

實施例:

你的控制器名稱是從Application模塊IndexController。工廠類是IndexControllerFactory

應用\控制器\ IndexControllerFactory

<?php 
namespace Application\Controller; 

use Zend\ServiceManager\Factory\FactoryInterface; 
use Interop\Container\ContainerInterface; 

class IndexControllerFactory implements FactoryInterface 
{ 
    public function __invoke(ContainerInterface $container, $requestedName, array $options = null) 
    { 
     $config = $container->get("Config"); 
     return new IndexController($config); 
    } 
} 

應用\控制器\的IndexController

<?php 
namespace Application\Controller; 

use Zend\Mvc\Controller\AbstractActionController; 

class IndexController extends AbstractActionController 
{ 
    private $config; 

    public function __construct(array $config) 
    { 
     $this->config = $config; 
    } 

    public function indexAction() 
    { 
     // use $this->config here 
    } 
} 

和這裏在module.config.php

'controllers' => [ 
     'factories' => [ 
      Controller\IndexController::class => Controller\IndexControllerFactory::class 
     ], 
    ], 
配置

希望這有助於

+0

不工作。 –

0

這是澄清

ZF3,如果要創建需要在應用程序中的任何類,讓他們維修,讓他們通過的ServiceManager在應用程序中使用。 ServiceManager實現了一個存儲註冊服務的容器。那是怎麼回事?採埃孚使用一種名爲的工廠(簡而言之,它創建了對象)。它有助於將服務存儲到容器中。然後,我們可以使用ServiceManager從該容器中提取服務。讓我們看看如何?

ServiceManager本身就是一項服務。

所以使用工廠讓我們在控制器可用的ServiceManager實例(例如,索引控制器)。這樣我們就可以使用它來獲得任何服務。

應用\控制器\ IndexControllerFactory

<?php 
namespace Application\Controller; 

// This is the container 
use Interop\Container\ContainerInterface; 
use Zend\ServiceManager\Factory\FactoryInterface; 

class IndexControllerFactory implements FactoryInterface 
{ 
    public function __invoke(ContainerInterface $container, $requestedName, array $options = NULL) 
    { 
     $serviceManager = $container->get('ServiceManager'); 
     return new IndexController($serviceManager); 
    }  
} 

讓我們來配置它,使我們可以使用它。請在moudle.config.php

'controllers' => [ 
    'factories' => [ 
     Controller\IndexController::class => Controller\IndexControllerFactory::class, 
    ], 
], 

一旦的IndexController通過IndexControllerFactory實例如下變化(通過以上配置)的的ServiceManager例如,通過索引控制器的構造變得可用。

<?php 
namespace Application\Controller; 

use Zend\Mvc\Controller\AbstractActionController; 
use Zend\View\Model\ViewModel; 
use Zend\ServiceManager\ServiceManager; 

class IndexController extends AbstractActionController 
{ 
    protected $serviceManager; 

    public function __construct(ServiceManager $serviceManager) 
    { 
     // Here we set the service manager instance 
     $this->serviceManager = $serviceManager; 
    } 

    public function indexAction() 
    { 
     // Use this as you want 
     $config = $this->serviceManager->get('config'); 

     return new ViewModel(); 
    } 

如果我們需要另一個類,而不是控制器內從配置服務的東西是什麼?例如,我們想要將圖像上傳到特定的目的地。那麼我們如何解決上傳路徑?看下面的例子。

我們將通過上載圖片RenameUpload過濾器。它有一個選項,名稱爲目標它指定上載路徑的目的地。我們爲上傳過濾器創建另一個工廠

應用\控制器\表格\過濾器\ UploadFilterFactory

<?php 
namespace Application\Form\Filter; 

use Interop\Container\ContainerInterface; 
use Zend\ServiceManager\Factory\FactoryInterface; 
use Application\Form\Filter\UploadFilter; 

class UploadFilterFactory implements FactoryInterface 
{ 

    public function __invoke(ContainerInterface $container, $requestedName, array $options = NULL) 
    { 
     $config = $container->get('config'); 

     // Look! here we fix the upload path 
     $uploadPath = $config['module_config']['upload_path']; 

     // Here we're injecting that path 
     return new UploadFilter($uploadPath); 
    } 
} 

做同樣的UploadForm,如果你需要。這將是UploadFormFactory

把下面的兩個片段在module.config.php。這是爲上傳過濾器工廠

'service_manager' => [ 
    'factories' => [ 
     // UploadForm::class => UploadFormFactory::class, 
     UploadFilter::class => UploadFilterFactory::class, 
    ], 

    // Make an alias so that we can use it where we need 
    // it could be uploadAction() inside any controller 
    // $inputForm = $this->serviceManager->get('UploadForm'); 
    // $inputFilter = $this->serviceManager->get('UploadFilter'); 
    // $uploadForm->setInputFilter($inputFilter), for example 
    'aliases' => [ 
     // 'UploadForm' => UploadForm::class, 
     'UploadFilter' => UploadFilter::class, 
    ], 
], 

,這一次爲上傳路徑,無論你想上傳。

'module_config' => [ 
    // Set the path as you want 
    'upload_path' => __DIR__ . '/../data/upload', 
], 

這是申請\表格\過濾器\ UploadFilter

<?php 
namespace Application\Form\Filter; 

use Zend\InputFilter\InputFilter; 
use Zend\Filter\File\RenameUpload; 

class UploadFilter extends InputFilter 
{ 
    protected $uploadPath; 

    public function __construct(string $uploadPath) 
    { 
     // We're assigning here so that we can use it 
     // on the filter section. 
     $this->uploadPath = $uploadPath; 

     $this->prepareFilters(); 
    } 

    public function prepareFilters() 
    { 
     $this->add(array(
      'name' => 'image', 
      'required' => true, 
      'filters' => array(
       array(
        'name' => RenameUpload::class, 
        'options' => array(
         // Thus here we use it 
         'target' => $this->uploadPath, 
         'overwrite' => true, 
         'randomize' => true, 
         'use_upload_extension' => true, 
        ),   
       ), 
      ), 
      'validators' => array(), 
     )); 
    } 
} 

這是使事情變得有用的一種方法。那麼爲什麼ServiceManager?這是爲了使分散的對象停止使用。它刪除隱藏的依賴關係。這使得代碼更簡潔易懂。原則是好設計