Zend Framework 2 Service Manager Configuration method

Source: Internet
Author: User

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.phpreturn array(   ‘service_manager‘=> array(       ‘factories‘=> array(           ‘translator‘=> ‘Zend\I18n\Translator\TranslatorServiceFactory‘,           ‘Application\Header\Navigation‘=> ‘Application\Navigation\HeaderNavigationFactory‘       ),       // 也可以添加其他服务   ),   // 省略其他module.config.php代码);
moduel.php
// Application/Module.phpclass Module{    publicfunction getServiceConfig(){        returnarray(            ‘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.phpreturn array(   ‘controllers‘=> array(        ‘invokables‘=> array(            ‘Application\Controller\Index‘=> ‘Application\Controller\IndexController‘,        )        // 也可以添加其他服务    ),   // 省略其他module.config.php代码);
module.php
// Application/Module.phpclass Module{    // 通过mvc自动调用,没必要通过get手动调用    publicfunction getControllerConfig()    {        returnarray(            ‘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.phpreturn 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.phpclass Module{    publicfunction getControllerPluginConfig()    {        returnarray(            ‘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.phpreturn 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.