We typically configure Service manager in two locations
1.module.config.php
2.module.php
Different service Manager types have different configuration methods
Application Services
Manager |
Application Services |
Manager class |
Zend\servicemanager\servicemanager |
Config Key |
Service_manager |
Module method |
Getserviceconfig () |
Module interface |
Serviceproviderinterface |
moduel.config.php
// Application/config/module.config.php
return array
(
‘service_manager‘
=>
array
(
‘factories‘
=>
array
(
‘translator‘
=>
‘Zend\I18n\Translator\TranslatorServiceFactory‘
,
‘Application\Header\Navigation‘
=>
‘Application\Navigation\HeaderNavigationFactory‘
),
// 也可以添加其他服务
),
// 省略其他module.config.php代码
);
|
moduel.php
// Application/Module.php
class Module{
public
function getServiceConfig(){
return
array
(
‘invokables‘
=>
array
( ),
// 也可以添加其他服务
);
}
// 省略其他Module.php代码
}
|
Controllers
Manager |
Controllers |
Manager class |
Zend\mvc\controller\controllermanager |
Config Key |
Controllers |
Module method |
Getcontrollerconfig () |
Module interface |
Controllerproviderinterface |
Service Name |
Controllerloader |
module.config.php
// Application/config/module.config.php
return array
(
‘controllers‘
=>
array
(
‘invokables‘
=>
array
(
‘Application\Controller\Index‘
=>
‘Application\Controller\IndexController‘
,
)
// 也可以添加其他服务
),
// 省略其他module.config.php代码
);
|
module.php
// Application/Module.php
class Module{
// 通过mvc自动调用,没必要通过get手动调用
public
function getControllerConfig()
{
return
array
(
‘invokables‘
=>
array
(
‘Application\Controller\Index‘
=>
‘Application\Controller\IndexController‘
,
),
// 也可以添加其他服务
);
}
// 省略其他Module.php代码
}
|
Controller Plugins
Manager |
Controller Plugins |
Manager class |
Zend\mvc\controller\pluginmanager |
Config Key |
Controller_plugins |
Module method |
Getcontrollerpluginconfig () |
Module interface |
Controllerpluginproviderinterface |
Service Name |
Controllerpluginmanager |
module.config.php
// Application/config/module.config.php
return array
(
‘controller_plugins‘
=>
array
(
‘factories‘
=>
array
(
‘MyModule\Controller\Plugin\Foo‘
=>
function
(
$sm
) {
$plugin
=
new Plugin\Foo;
$cache
=
$sm
->get(
‘my-cache‘
);
$plugin
->setCache(
$cache
);
return
$plugin
;
},
),
// 也可以添加其他服务
),
// 省略其他module.config.php代码
);
|
module.php
// Application/Module.php
class Module{
public
function getControllerPluginConfig()
{
return
array
(
‘invokables‘
=>
array
(
// ...
),
// 也可以添加其他服务
);
}
// 省略其他Module.php代码
}
|
View Helpers
Manager |
View Helpers |
Manager class |
Zend\view\helperpluginmanager |
Config Key |
View_helpers |
Module method |
Getviewhelperconfig () |
Module interface |
Viewhelperproviderinterface |
Service Name |
Viewhelpermanager |
module.config.php
// Application/config/module.config.php
return array
(
‘view_helpers‘
=>
array
(
‘factories‘
=>
array
(
‘ApplicationHelper‘
=>
function
(
$helperPluginManager ) {
// ....
}
)
// 也可以添加其他服务
),
// 省略其他module.config.php代码
);
|
module.php
// Application/Module.php
class
Module{
public
function
getViewHelperConfig()
{
return
array
(
‘factories‘
=>
array
(
‘ApplicationHelper‘
=>
function
(
$helperPluginManager
) {
// ....
}
),
// 也可以添加其他服务
);
}
// 省略其他Module.php代码
}
Zend Framework 2 Service Manager Configuration method