简体   繁体   English

锂框架:如何在布局中获取控制器名称

[英]Lithium framework: how to obtain controller name in layout

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). 我在一个锂电池布局文件中,我想回显当前控制器的名称(以后用作CSS类)。 How can I obtain the current controller name? 如何获取当前控制器名称?

Thanks, aeno 谢谢,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/ 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. 要获取url的“区域”部分,等等。

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: 以下代码可在任何Lithium布局或视图中使用,以查找当前的Controller,将其转换为合适的CSS类名称,并将其设置为div的class属性:

<?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 请求类记录在这里: http : //li3.me/docs/lithium/action/Request

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM