php Kohana 3 获取当前控制器/动作/参数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/2763920/
Warning: these are provided under cc-by-sa 4.0 license.  You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Kohana 3 get current controller/action/arguments
提问by n00b
In Kohana 2 you could easily get that information like this:
在 Kohana 2 中,您可以轻松获得如下信息:
echo router::$controller;
echo router::$method;
echo router::$arguments[0-x];
Any idea how that works in Kohana 3?
知道它在 Kohana 3 中是如何工作的吗?
Thanks in advance!
提前致谢!
回答by Matt
From inside a controller:
从控制器内部:
$this->request->controller
$this->request->controller
$this->request->action
$this->request->action
$this->request->param('paramname')
$this->request->param('paramname')
Unlike K2 arguments in K3 are accessed via kays which you define in your routes.
与 K3 中的 K2 参数不同,通过您在路由中定义的 kay 访问。
Take for example this url:
以这个网址为例:
Route::set('default', '(<controller>(/<action>(/<id>)))')    
    ->defaults(array('controller' => 'welcome', 'action' => 'index')); 
To access the "id" argument you'd call
要访问您要调用的“id”参数
$this->request->param('id')
$this->request->param('id')
You can't access the controller / action arguments from the param() method.
您无法从 param() 方法访问控制器/动作参数。
Note, you can also use Request::instance()to get the global (or "master") request instance.
请注意,您还可以使用Request::instance()获取全局(或“主”)请求实例。
For more information see the K3 guide
有关更多信息,请参阅K3 指南
回答by Yarin
Updated answer for Kohana 3.2, from the user guide:
来自用户指南的Kohana 3.2更新答案:
// From within a controller:
$this->request->action();
$this->request->controller();
$this->request->directory();
// Can be used anywhere:
Request::current()->action();
Request::current()->controller();
Request::current()->directory();
回答by Plankje
For those using Kohana >= 3.1, it might be useful to notice that some properties of the Request object have been converted to methods.
对于那些使用 Kohana >= 3.1 的人来说,注意到 Request 对象的某些属性已转换为方法可能会很有用。
E.g. Request::controlleris now Request::controller()(or $this->request->controller()when you're inside a controller).
例如Request::controller现在Request::controller()(或$this->request->controller()当您在控制器内时)。
For more information, I'd like to reference to the Kohana upgrade guide on http://kohanaframework.org/3.1/guide/kohana/upgrading
有关更多信息,我想参考http://kohanaframework.org/3.1/guide/kohana/upgrading上的 Kohana 升级指南

