How to get the controller name, action name in Zen

2019-02-19 03:01发布

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.

2条回答
欢心
2楼-- · 2019-02-19 03:35

Try as below for ZF2

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

$this->getEvent()->getRouteMatch()->getParam('controller', 'index');
查看更多
家丑人穷心不美
3楼-- · 2019-02-19 03:36

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));
查看更多
登录 后发表回答