ZF2 - Get controller name into layout/views

前端 未结 9 1467
逝去的感伤
逝去的感伤 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:16

    ZF2 is out and so is the skeleton. This is adding on top of the skeleton so it should be your best example:

    Inside Module.php

    public function onBootstrap($e)
    {
        $e->getApplication()->getServiceManager()->get('translator');
        $e->getApplication()->getServiceManager()->get('viewhelpermanager')->setFactory('controllerName', function($sm) use ($e) {
            $viewHelper = new View\Helper\ControllerName($e->getRouteMatch());
            return $viewHelper;
        });
    
        $eventManager        = $e->getApplication()->getEventManager();
        $moduleRouteListener = new ModuleRouteListener();
        $moduleRouteListener->attach($eventManager);
    }
    

    The actual ViewHelper:

    // Application/View/Helper/ControllerName.php
    
    namespace Application\View\Helper;
    
    use Zend\View\Helper\AbstractHelper;
    
    class ControllerName extends AbstractHelper
    {
    
    protected $routeMatch;
    
        public function __construct($routeMatch)
        {
            $this->routeMatch = $routeMatch;
        }
    
        public function __invoke()
        {
            if ($this->routeMatch) {
                $controller = $this->routeMatch->getParam('controller', 'index');
                return $controller;
            }
        }
    }
    

    Inside any of your views/layouts

    echo $this->controllerName()
    
    0 讨论(0)
  • 2020-11-30 07:17

    I created CurrentRoute View Helper for this purpose.

    Install it:

    composer require tasmaniski/zf2-current-route
    

    Register module in config/application.config.php:

    'modules' => array(
        '...',
        'CurrentRoute'
    ),
    

    Use it in any view/layout file:

    $this->currentRoute()->getController();  // return current controller name
    $this->currentRoute()->getAction();      // return current action name
    $this->currentRoute()->getModule();      // return current module name
    $this->currentRoute()->getRoute();       // return current route name
    

    You can see full documentation and code https://github.com/tasmaniski/zf2-current-route

    0 讨论(0)
  • 2020-11-30 07:21

    Short Code here :

    $this->getHelperPluginManager()->getServiceLocator()->get('application')->getMvcEvent()->getRouteMatch()->getParam('action', 'index');
    
    $controller = $this->getHelperPluginManager()->getServiceLocator()->get('application')->getMvcEvent()->getRouteMatch()->getParam('controller', 'index');
    
    $controller = array_pop(explode('\', $controller));
    
    0 讨论(0)
  • 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;
                },
             ),
       );
    }
    
    0 讨论(0)
  • 2020-11-30 07:23

    Get controller / action name in controller in Zend-3 framework

    private function getControllerActionName()
    {
        $currentController = $this->getEvent()->getRouteMatch()->getParam('controller', 'index');
        $explode_controller = explode('\\', $currentController);
        $currentController = strtolower(array_pop($explode_controller));
        $currentController = str_replace('controller', '', $currentController);
        $currentAction = strtolower($this->getEvent()->getRouteMatch()->getParam('action', 'index'));
        return array(
                'controller' => $currentController,
                'action' => $currentAction,
            );
    }
    

    It works for me. I hope, this will also help you. Thanks for asking this question.

    0 讨论(0)
  • 2020-11-30 07:32

    In zf2 beta4 it made in this manner:

    public function init(ModuleManager $moduleManager)
    {
    
        $sharedEvents = $moduleManager->events()->getSharedManager();
        $sharedEvents->attach('bootstrap', 'bootstrap', array($this, 'onBootstrap'));
    }
    
    public function onBootstrap($e)
    {
        $app     = $e->getParam('application');
        // some your code here
        $app->events()->attach('route', array($this, 'onRouteFinish'), -100);
    }
    
    public function onRouteFinish($e)
    {
         $matches    = $e->getRouteMatch();
         $controller = $matches->getParam('controller');
         var_dump($controller);die();
    }
    
    0 讨论(0)
提交回复
热议问题