>>在自定义任意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;
}
}