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

发布于 2024-12-11 08:08:43 字数 70 浏览 6 评论 0原文

我在一个锂布局文件中,我想回显当前控制器的名称(稍后用作 CSS 类)。如何获取当前控制器名称?

谢谢, 艾诺

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

埋葬我深情 2024-12-18 08:08:43

我想你的意思是你在视图中?

如果是这样,那么获取控制器或路由/请求的其他部分非常简单......

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

这将为您提供控制器,但您可以从您的路线中获取您需要的任何内容。因此,假设您有一条像...这样的路线,

Router::connect('/{:controller}/{:action}/{:id}');

您可以在您的视图中使用以下两种方式:

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

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

或者您可以有一条更漂亮的路线,例如..

Router::connect('/{:id}/{:area}/{:controller}/{:action}/');

这将适用于像这样的网址:

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

现在您可以做类似的事情...

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

获取网址的“区域”部分等。您明白了。

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.

独守阴晴ぅ圆缺 2024-12-18 08:08:43

可以在任何 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>

请求类记录在此处: http://li3.me/docs/lithium/action/Request

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文