문제

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

도움이 되었습니까?

해결책

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:

http://mysite.com/123/media/photos/edit/

Now you can do something like ...

<?=$this->_request->area;?> 

To get the "area" portion of the url, etc. You get the idea.

다른 팁

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

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top