Kohana 3获得当前的控制器/动作/参数

n00*_*00b 18 php kohana kohana-3

在Kohana 2中,您可以轻松获得如下信息:

echo router::$controller;
echo router::$method;
echo router::$arguments[0-x];
Run Code Online (Sandbox Code Playgroud)

知道Kohana 3的效果如何?

提前致谢!

Mat*_*att 32

从控制器内部:

$this->request->controller

$this->request->action

$this->request->param('paramname')

与K2中的参数不同,K3是通过您在路线中定义的kays访问的.

以这个网址为例:

Route::set('default', '(<controller>(/<action>(/<id>)))')    
    ->defaults(array('controller' => 'welcome', 'action' => 'index')); 
Run Code Online (Sandbox Code Playgroud)

要访问您要调用的"id"参数

$this->request->param('id')

您无法从param()方法访问控制器/操作参数.

注意,您还可以使用Request::instance()获取全局(或"主")请求实例.

有关更多信息,请参阅K3指南


Yar*_*rin 25

用户指南更新的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();
Run Code Online (Sandbox Code Playgroud)