Reputation: 560
I'm in a lithium layout file and I'd like to echo the name of the current controller (to use as a CSS class later). How can I obtain the current controller name?
Thanks, aeno
Upvotes: 2
Views: 1195
Reputation: 36
The following code can be used in any Lithium layout or view to look up the current Controller, convert it to a suitable CSS class name, and set it as the class attribute of a div:
<?php
$controller = $this->request()->controller;
$controller_css_class = strtolower(\lithium\util\Inflector::slug($controller));
?>
<div class="<?=$controller_css_class; ?>"></div>
The request class is documented here: http://li3.me/docs/lithium/action/Request
Upvotes: 0
Reputation: 27080
I assume you mean you are in a View?
If so, it's pretty simple to get the controller or other parts of the route/request ...
<?=$this->_request->controller;?>
That will get you the Controller but you can get just about anything from your route you'd need. So assuming you have a route like ...
Router::connect('/{:controller}/{:action}/{:id}');
You can use both of the following in your view:
<?=$this->_request->action;?>
<?=$this->_request->id;?>
Or you could have a fancier route like ..
Router::connect('/{:id}/{:area}/{:controller}/{:action}/');
This would be for a url like:
Now you can do something like ...
<?=$this->_request->area;?>
To get the "area" portion of the url, etc. You get the idea.
Upvotes: 4