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.
U
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()
I created CurrentRoute View Helper for this purpose.
Install it:
composer require tasmaniski/zf2-current-route
Register module in config/application.config.php:
'modules' => array(
'...',
'CurrentRoute'
),
Use it in any view/layout file:
$this->currentRoute()->getController(); // return current controller name
$this->currentRoute()->getAction(); // return current action name
$this->currentRoute()->getModule(); // return current module name
$this->currentRoute()->getRoute(); // return current route name
You can see full documentation and code https://github.com/tasmaniski/zf2-current-route
Short Code here :
$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));
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;
},
),
);
}
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.
In zf2 beta4 it made in this manner:
public function init(ModuleManager $moduleManager)
{
$sharedEvents = $moduleManager->events()->getSharedManager();
$sharedEvents->attach('bootstrap', 'bootstrap', array($this, 'onBootstrap'));
}
public function onBootstrap($e)
{
$app = $e->getParam('application');
// some your code here
$app->events()->attach('route', array($this, 'onRouteFinish'), -100);
}
public function onRouteFinish($e)
{
$matches = $e->getRouteMatch();
$controller = $matches->getParam('controller');
var_dump($controller);die();
}