ZF2 - Get controller name into layout/views

前端 未结 9 1562
逝去的感伤
逝去的感伤 2020-11-30 07:03

I know with ZF1 you would retrieve the module/controller name using custom View Helpers that would get the singleton frontController object and get the name there.

U

9条回答
  •  借酒劲吻你
    2020-11-30 07:22

    instead of extending onBootStrap() in Module.php, you can use getViewHelperConfig() (also in Module.php). The actual helper is unchanged, but you get the following code to create it:

    public function getViewHelperConfig()
    {
       return array(
             'factories' => array(
                'ControllerName' => function ($sm) {
                   $match = $sm->getServiceLocator()->get('application')->getMvcEvent()->getRouteMatch();
                   $viewHelper = new \Application\View\Helper\ControllerName($match);
                   return $viewHelper;
                },
             ),
       );
    }
    

提交回复
热议问题