Kohana 中的 Request::$controller
为什么我无法从视图请求使用的控制器的名称?
例如,someview.php 包含:
Kohana 显示错误:“ErrorException [ Fatal Error ]: Access to undeclared static property: Request::$controller”
为什么?怎么了?
我需要这样做:
; Example.com示例.com
Why I cannot request a used controller's name from the View?
For example, someview.php contains:
<?php
echo Request::$controller;
?>
Kohana shows the error: “ErrorException [ Fatal Error ]: Access to undeclared static property: Request::$controller”
Why? What's wrong?
It is needed for me for doing this:
<?php if (Request::$controller != 'index') { ?>
<a href="/">Example.com</a>
<?php } else { ?>
Example.com
<?php } ?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
相反,在控制器上执行此操作:
然后您可以在任何视图上访问
$controller
。Do this instead, on the controller :
Then you can access
$controller
on any view.请求应该通过它的静态方法访问,不需要定义额外的静态属性 ||全局视图变量来获取它。
Request::instance()
将返回主请求实例(“母实例”)。Request::current()
将返回当前活动请求的实例,与您可以在 Controller 中使用$this->request
访问的内容相同。Request should be accessed by it's static methods, there's no need to define additional static properties || global view vars to get it.
Request::instance()
will return the the main request instance ('mother instance').Request::current()
will return the instance of the currently active request, the same thing you can access with$this->request
in Controller.我会按照尤达的建议去做,但是我可能也会将该逻辑放入控制器中。
我猜你想要一个返回主页的链接?
也不要忘记,您可以使用
Route::get()
或其朋友之一从您的路线构建链接。I would do as yoda suggested, however I would probably put that logic in the controller as well.
I assume you want a link back to home?
Don't forget too you can build links from your routes by using
Route::get()
or one of its friends.在 Kohana 3.1 中
给出“ErrorException [通知]:未定义的属性:Request::$controller”。然后我只是使用 Request::current()->controller() 来查看可接受/最佳实践/最佳性能?
In Kohana 3.1
give the "ErrorException [ Notice ]: Undefined property: Request::$controller". then i simply using Request::current()->controller() in view acceptable/ best practice/ optimal performance?
Kohana 3.2:在控制器中,粘贴这个(我发现你不能使用bind_global真的很愚蠢)
然后在视图中,你可以使用:
Kohana 3.2: In the controller, paste this (I find it really really stupid that you can't use bind_global)
Then in the view, you can use: