如何在Zend Framework 2中获取控制器名称,操作名称

时间:2012-10-14 15:25:01

标签: php zend-framework2

我在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();
    }
}

如何获取当前控制器或操作名称的名称...并将其传递给视图和/或布局? 不得不说我刚刚开始使用ZF2框架。

3 个答案:

答案 0 :(得分:18)

请尝试以下ZF2

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

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

答案 1 :(得分:4)

这适用于我的项目:

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

答案 2 :(得分:0)

在Zend-3框架中的控制器中获取控制器/动作名称

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,
        );
}

对我有用。希望对您有帮助。感谢您提出这个问题:)