Kohana param()不起作用

时间:2010-07-19 06:26:28

标签: kohana kohana-3

我正在使用Kohana 3.有谁知道为什么param('controller')结果为NULL。

路由:

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

网址:http://localhost/application/page/index/1

Params电话:

$param = Request::instance()->param('controller');
echo Kohana::debug($param); //results: NULL
$param = Request::instance()->param('action');
echo Kohana::debug($param); //results: NULL
$param = Request::instance()->param('id');
echo Kohana::debug($param); //results: 1

2 个答案:

答案 0 :(得分:6)

在622行的request.php中查找:

// These are accessible as public vars and can be overloaded
unset($params['controller'], $params['action'], $params['directory']);

// Params cannot be changed once matched
$this->_params = $params;

这就是为什么第695行无法返回controller

public function param($key = NULL, $default = NULL)
{
    return $this->_params[$key];
}

如果您在控制器内部,这就是控制器$controller = Request::instance()->controller;$controller = $this->request->controller;的方式

答案 1 :(得分:3)

对于使用Kohana 3.1的每个人,在控制器中访问当前控制器的名称和这样的动作:

$this->request->controller()

$this->request->action()

或者如果您不在控制器中,您可以随时访问当前请求的方法,如下所示:Request::current()->controller()

有关您可以同样访问的更多方法,请参阅system/classes/kohana/request.php