内部和外部的不同请求–kohana3使用手册
当使用 Kohana HMVC的时候,它能很完美的区分来自外部(那些来自客户端的)和内部(你的应用程序内部制造的子请求)的请求。我们来验证下的模板控制器中的 before() 方法内的请求是来自内部还是外部。
class Controller_Sample extends Controller_Template {
public $internal = FALSE;
public function before()
{
parent::before();
if ($this->request != Request::instance()) {
$this->internal = TRUE;
}
}
}
另外这里有个很好的技巧来重新定义函数,以便你来区分内部或者是外部的请求。
class Controller_Sample extends Controller {
public function before()
{
if ($this->internal == TRUE) {
$this->request->action = 'internal_'.$this->request->action;
}
}
public function action_index()
{
// 如果请求时外部的,这个动作将被调用
}
public function action_internal_index()
{
//如果请求时内部的,这个动作将被调用
}
}
相当有用,是吧?你可以构建子请求的页面,同时你也可以发送 ajax 或者 HTTP 请求道指定控制器。
讨论
1. “$this->request === Request::instance()”有能力测试出请求来自内部吗?
2. 是的,这些不需要继承 Request 类,这是多余的。