3

我在 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框架入手。

4

3 回答 3

19

尝试如下 ZF2

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

$this->getEvent()->getRouteMatch()->getParam('controller', 'index');
于 2012-10-14T15:29:10.410 回答
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));
于 2014-04-17T08:20:10.470 回答
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,
        );
}

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

于 2019-11-23T13:14:27.247 回答