How to get the controller name, action name in Zend Framework 2

点点圈 提交于 2019-12-01 06:14:53

问题


I have a default module in Zend Framework 2:

namespace Application\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;

class IndexController extends AbstractActionController
{
    public function indexAction()
    {
        return new ViewModel();
    }
}

How can I get the name of the current controller or action name ... and pass it to the view and/or layout? Have to say that I am just starting with the ZF2 framework.


回答1:


Try as below for ZF2

$this->getEvent()->getRouteMatch()->getParam('action', 'index'); 

$this->getEvent()->getRouteMatch()->getParam('controller', 'index');



回答2:


This works on my project:

$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));



回答3:


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 :)



来源:https://stackoverflow.com/questions/12883598/how-to-get-the-controller-name-action-name-in-zend-framework-2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!