Zend Framework2(zf2) 在不同模块中调用全局配置方法

本文详细介绍了如何在自定义Class中实现ServiceLocatorAwareInterface,通过Module.php或module.config服务管理器注册来获取配置对象,并展示了在不同场景下如何灵活使用配置。包括在Module.php配置getServiceConfig()方法,控制器、视图中获取配置文件数组,以及Model中获取配置的实践案例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

>>在自定义任意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'键名须按规范设置,
                 *以保证其它模块对Admin\Model\AdminTable类的正确调用
                 * return $table注入到类Admin\Model\AdminTable
                 */
                '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;
            }
           }
        

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值