18

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

echo router::$controller;
echo router::$method;
echo router::$arguments[0-x];

知道这在 Kohana 3 中是如何工作的吗?

提前致谢!

4

3 回答 3

32

从控制器内部:

$this->request->controller

$this->request->action

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

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

以这个网址为例:

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

要访问您调用的“id”参数

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

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

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

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

于 2010-05-04T09:51:21.180 回答
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();
于 2011-11-04T20:10:27.977 回答
3

对于使用 Kohana >= 3.1 的用户,请注意 Request 对象的某些属性已转换为方法可能会很有用。

例如Request::controller现在Request::controller()(或$this->request->controller()当您在控制器内时)。

有关更多信息,我想参考http://kohanaframework.org/3.1/guide/kohana/upgrading上的 Kohana 升级指南

于 2011-07-28T19:49:22.213 回答