我们通常会把Service Manager配置在两个地方
1.module.config.php
2.Module.php
不同的service manager 类型有不同的配置方法
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代码
}