Zend Framework 2 Service Manager 配置方法
我们通常会把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
return array (
'service_manager' => array (
'factories' => array (
'translator' => 'Zend\I18n\Translator\TranslatorServiceFactory' ,
'Application\Header\Navigation' => 'Application\Navigation\HeaderNavigationFactory'
),
),
);
|
Moduel.php
class Module{
public function getServiceConfig(){
return array (
'invokables' => array ( ),
);
}
}
|
Controllers
Manager | Controllers |
Manager class | Zend\Mvc\Controller\ControllerManager |
Config key | controllers |
Module method | getControllerConfig() |
Module interface | ControllerProviderInterface |
Service name | ControllerLoader |
module.config.php
return array (
'controllers' => array (
'invokables' => array (
'Application\Controller\Index' => 'Application\Controller\IndexController' ,
)
),
);
|
Module.php
class Module{
public function getControllerConfig()
{
return array (
'invokables' => array (
'Application\Controller\Index' => 'Application\Controller\IndexController' ,
),
);
}
}
|
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
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.php
class Module{
public function getControllerPluginConfig()
{
return array (
'invokables' => array (
),
);
}
}
|
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
return array (
'view_helpers' => array (
'factories' => array (
'ApplicationHelper' => function ( $helperPluginManager ) {
}
)
),
);
|
Module.php
class
Module{
public
function
getViewHelperConfig()
{
return
array
(
'factories'
=>
array
(
'ApplicationHelper'
=>
function
(
$helperPluginManager
) {
}
),
);
}
}