3

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.

Robert Kuzma
  • 143
  • 1
  • 4
  • 9
  • Did you take a look at http://stackoverflow.com/questions/8843092/zf2-get-controller-name-into-layout-views? – DrBeza Oct 14 '12 at 15:29

3 Answers3

19

Try as below for ZF2

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

$this->getEvent()->getRouteMatch()->getParam('controller', 'index');
GBD
  • 15,847
  • 2
  • 46
  • 50
4

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));
Rahil Wazir
  • 10,007
  • 11
  • 42
  • 64
James Labs
  • 71
  • 1
0

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

Kamlesh
  • 5,233
  • 39
  • 50