Zend Framework2(zf2) 在不同模块中调用全局配置方法
2015-03-22 10:24
344 查看
>>在自定义任意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;
}
}
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;
}
}
相关文章推荐
- dwr 3.0 配置 + 不同参数,不同返回值调用方法 示例
- Android根据不同身份配置APP对应的不同模块方法
- Struts2.xml文件中的常量、全局错误的配置及相关动态方法的调用
- python利用字典保存配置实现动态调用模块类方法
- servlet都有一个servletConfig对象;四个config对象可以调用的方法;ServletContext对象之获取web项目信息;设置全局初始化参数的配置
- dwr 3.0 配置 + 不同参数,不同返回值调用方法 示例
- Node.js模块全局安装路径配置方法
- Zend Framework2 - Layout配置,不同模块Module设置不同的layout
- dwr 3.0 配置 + 不同参数,不同返回值调用方法 示例
- 用js实现多域名不同文件的调用方法
- webservice的配置动态调用方法 [转]
- C#多线程-不同线程之间通过事件委托封送调用方法
- webservice的配置动态调用方法
- Enterprise Library3.1 使用数据访问模块时,调用Microsoft.Practices.EnterpriseLibrary.Data报出源文件与当前应用程序不一致和创建dataconfiguration的配置节处理程序出错
- 不同的JS文件之间方法的调用
- Cisco在不同系统下的VLAN配置方法
- 不同编码网站内容之间的JS调用方法
- C#多线程-不同线程之间通过事件委托封送调用方法
- C# WinForm (笨方法)根据不同的样式配置 设置窗体相关控件的背景 以改变窗体风格
- SUN ONE APPSERVER 7 连接池和数据源的配置方法和调用