Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ZF2 - Get controller name into layout/views

I know with ZF1 you would retrieve the module/controller name using custom View Helpers that would get the singleton frontController object and get the name there.

Using ZF2 as they've abolished alot of the singleton nature of the framework and introduced DI where I've specified aliases for all of my controllers within this module... I can imagine I would get it through accessing the DI or perhaps injecting the current name into the layout.

Anyone got any idea how you would do it. I guess there a hundred different ways but after sniffing about the code for a few hours I can't really figure out how its meant to be done now.

The reason I wanted the controller name is to add it to the body as a class for specific controller styling.

Thanks, Dom

like image 849
Intellix Avatar asked Jan 12 '12 22:01

Intellix


Video Answer


3 Answers

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('translator');
    $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();
    $moduleRouteListener->attach($eventManager);
}

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()
like image 139
Intellix Avatar answered Oct 04 '22 16:10

Intellix


This would be a solution I got to work with zf2 beta5

module/MyModule/Module.php

namespace MyModule;

use Zend\Mvc\ModuleRouteListener;
use MyModule\View\Helper as MyViewHelper;

class Module
{
    public function onBootstrap($e)
    {
        $app = $e->getApplication();
        $serviceManager = $app->getServiceManager();

        $serviceManager->get('viewhelpermanager')->setFactory('myviewalias', function($sm) use ($e) {
            return new MyViewHelper($e->getRouteMatch());
        });
    }
    ...
}

module/MyModule/src/MyModule/View/Helper.php

namespace MyModule\View;

use Zend\View\Helper\AbstractHelper;

class Helper extends AbstractHelper
{

    protected $route;

    public function __construct($route)
    {
        $this->route = $route;
    }

    public function echoController()
    {
        $controller = $this->route->getParam('controller', 'index');
        echo $controller;
    }
}

In any viewfile

$this->myviewalias()->echoController();
like image 43
Borje Avatar answered Oct 04 '22 16:10

Borje


instead of extending onBootStrap() in Module.php, you can use getViewHelperConfig() (also in Module.php). The actual helper is unchanged, but you get the following code to create it:

public function getViewHelperConfig()
{
   return array(
         'factories' => array(
            'ControllerName' => function ($sm) {
               $match = $sm->getServiceLocator()->get('application')->getMvcEvent()->getRouteMatch();
               $viewHelper = new \Application\View\Helper\ControllerName($match);
               return $viewHelper;
            },
         ),
   );
}
like image 5
dstj Avatar answered Oct 04 '22 15:10

dstj