Access to module config in Zend Framework 2

前端 未结 8 2347
清酒与你
清酒与你 2021-01-30 21:05

How I can get access to my module config from the controller?

8条回答
  •  忘掉有多难
    2021-01-30 21:48

    To read module-only config your module should just implement LocatorRegisteredInterface

    Before:

    namespace Application;
    
    class Module
    {
       // ...
    }
    

    After:

    namespace Application;
    
    use Zend\ModuleManager\Feature\LocatorRegisteredInterface;
    
    class Module implements LocatorRegisteredInterface
    {
       // ...
    }
    

    That implementation says LocatorRegistrationListener to save module intance in service locator as namespace\Module

    Then anywhere you can get access to your module:

    class IndexController extends AbstractActionController
    {
        public function indexAction()
        {
            /** @var \Application\Module $module */
            $module = $this->getServiceLocator()->get('Application\Module');
            $moduleOnlyConfig = $module->getConfig();
    
            // ...
        }
    } 
    

提交回复
热议问题