天天看點

Zend Framework2(zf2) 在不同子產品中調用全局配置方法

>>在自定義任意Class中 (包括Model類)擷取配置資訊:隻要實作 ServiceLocatorAwareInterface

use Zend\ServiceManager\ServiceLocatorAwareInterface;

use Zend\ServiceManager\ServiceLocatorInterface;

class AdminTable implements ServiceLocatorAwareInterface{

    protected $tableGateway;    

    protected $serviceLocator;

    //$tg 由Module.php 配置 getServiceConfig()注入

    public function __construct(TableGateway $tg)

    {

        $this->tableGateway = $tg;    //實作ServiceLocatorAwareInterface可以忽略

    }

    public function setServiceLocator(ServiceLocatorInterface $serviceLocator) {

        $this->serviceLocator = $serviceLocator;

    }

    public function getServiceLocator() {

        return $this->serviceLocator;

    }

   >>然後必須在Module.php 或 module.config 服務管理器中注冊才能傳回配置對象,

   注意:在__construct中傳回值null,必須在init()運作後擷取配置,

  調用:$form = $this->getServiceLocator()->get('Admin\Form\ColumnForm'),

  不可直接new ColumnForm(),否則不能調用類中的服務管理對象$sm

  eg:

  >>在module.config

  'service_manager'=>array(

        'invokables' => array(

            'Admin\Form\ColumnForm' => 'Admin\Form\ColumnForm',

        ),

    ),

  >>在module.php

          'Admin\Form\ColumnForm' => function () {

            $form = new Form\ColumnForm();  //傳回Table對象,而非資料集

            return $form;

        },       

    public function fetchAll($paginated=false)

    {

        var_dump($this->serviceLocator->get('config')['db']);

   }

}

>>在Module.php 擷取相關配置資訊,隻要實作 public function getServiceConfig() 方法

      public function getServiceConfig(){        

        return array(

            'factories'=>array(

                'Admin\Model\AdminTable'=>function ($sm){

                    $tg = $sm->get('AdminTableGateway');                    

                    $table = new AdminTable($tg);

                    return $table;

                },

                'AdminTableGateway'=>function ($sm){

                    $config = $sm->get('config');    //'config'系統參數擷取配置檔案數組

                    $config = $config['db'];

                    print_r($config);

                    $dbFeature = new TableNamePrefixFeature('lf_');

                    $adapter = $sm->get('Zend\Db\Adapter\Adapter');

                    $rs = new ResultSet();

                    $rs->setArrayObjectPrototype(new Admin());

                    return new TableGateway('admin',$adapter, $dbFeature, $rs);

                },

            ),

        );

    }

  >>>  在控制器中擷取配置檔案數組,不可在構造函數中調用__construct

       $this->getServiceLocator()->get('Config');

  >>> 在視圖中擷取配置檔案

   view.phtml

   $sm = $this->getHelperPluginManager()->getServiceLocator()

>>> 在視圖助手Helper擷取配置

  $sm = $this->getView()->getHelperPluginManager()->getServiceLocator();

   擷取控制器、動作名稱

  $sm->get('Application')->getMvcEvent()->getRouteMatch()->getParam('action')  //action or controller

或者從控制器中擷取

   <?php

    namespace YourModule\Controller;

    use Zend\View\Model\ViewModel;

    // ...

    public function anyAction()

    {

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

        $viewModel = new ViewModel();

        $viewModel->setVariables(array('config' => $config ));

        return $viewModel;

    }

    // ...

    ?>

    So in your view.phtml file;

    <div class="foo">

     ...

     <?php echo $this->config; ?>

     ...

    </div>

    >>> 在Model擷取配置

     (1)在 Module.php配置

        public function getServiceConfig(){        

        return array(

            'factories'=>array(

                'Admin\Model\AdminTable'=>function ($sm){   

                    $tg = $sm->get('AdminTableGateway');

                    $table = new AdminTable($tg, $sm->get('config'));                    

                    return $table;

                },

            ),

        );

      (2)在Model調用

           >>注入$gc

          class AdminTable{

            protected $tableGateway;

            protected $getConfig;

            //$tg 由Module.php 配置 getServiceConfig()注入

            public function __construct(TableGateway $tg, array $gc=null)

            {

                $this->tableGateway = $tg;

                $this->getConfig = $gc;

            }

           }