I need to know in the main layout, if current page is like "module/controller". So, for instance, if I am on the page like "site.com/module/controller", the layout which is in "www/module/application/view/layout/layout.phtml" has to understand that I am on that page.
Could you tell me please, how to cope with it?
Thank you.
A possible solution for that is to set the route parameters as layout variables from your Module.php
.
Assuming you have a route configuration which looks like this :
'route' => '/[:module[/:controller[/:action]]]
You can use the following code :
In your Module.php
file :
public function onBootstrap($e)
{
//....
$eventManager = $e->getApplication()->getEventManager();
$eventManager->attach('dispatch', array($this, 'initView' ));
//....
}
public function initView(MvcEvent $e)
{
$controller = $e->getTarget();
$route = $controller->getEvent()->getRouteMatch();
//set variables into the layout
$controller->layout()->modulename = $route->getParam('module');
$controller->layout()->controllername = $route->getParam('controller');
$controller->layout()->actionname= $route->getParam('action');
}
Note : If you haven't module
in the route parameter you can get it as follows :
$controllerClass = get_class($controller);
$moduleName = substr($controllerClass, 0, strpos($controllerClass, '\\'));
$controller->layout()->modulename = $moduleName;
In your layout :
Now you can access the variables in your layout just like this :
Current page :<?php echo $this->modulename.'/'.$this->controllername.'/'.$this->actionname; ?>
Example :
Page : site.com/application/index/home
Output : Current page : Application/index/home
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.