ZF2 – Get controller name into layout/views

ZF2 is out and so is the skeleton. This is adding on top of the skeleton so it should be your best example:

Inside Module.php

public function onBootstrap($e)
    $e->getApplication()->getServiceManager()->get('viewhelpermanager')->setFactory('controllerName', function($sm) use ($e) {
        $viewHelper = new View\Helper\ControllerName($e->getRouteMatch());
        return $viewHelper;

    $eventManager        = $e->getApplication()->getEventManager();
    $moduleRouteListener = new ModuleRouteListener();

The actual ViewHelper:

// Application/View/Helper/ControllerName.php

namespace Application\View\Helper;

use Zend\View\Helper\AbstractHelper;

class ControllerName extends AbstractHelper

protected $routeMatch;

    public function __construct($routeMatch)
        $this->routeMatch = $routeMatch;

    public function __invoke()
        if ($this->routeMatch) {
            $controller = $this->routeMatch->getParam('controller', 'index');
            return $controller;

Inside any of your views/layouts

echo $this->controllerName()

Leave a Comment