在Kohana中控制器中实例化请求3.3

时间:2012-12-10 19:12:29

标签: request kohana kohana-3.3

升级我自己的模块以使用最新的Kohana(3.3)时,我发现我的方案出现故障。我在我的应用程序中使用模板驱动架构(我的控制器扩展了Controller_Theme)。但对于AJAX调用,我在版本3.2中使用了单独的Controller,它只扩展了Controller。我必须在此控制器中实例化Request对象,以便通过Rquest对象中的POST或GET访问传递的变量。我在__construct()方法中做到了:

class Controller_Ajax extends Controller {

    public function __construct()
    {       
        $this->request = Request::current();    
    }

    public function action_myaction()
    {
        if($this->is_ajax())
        {
            $url = $this->request->post('url');
            $text = $this->request->post('text');
        }   
    }
}

在myaction()方法中,我可以像这样访问已发布的变量。 但这在Kohana 3.3中不再起作用了。我总是得到这个错误:

ErrorException [ Fatal Error ]: Call to a member function action() on a non-object
SYSPATH/classes/Kohana/Controller.php [ 73 ]
68  {
69      // Execute the "before action" method
70      $this->before();
71      
72      // Determine the action to use
73      $action = 'action_'.$this->request->action();
74 
75      // If the action doesn't exist, it's a 404
76      if ( ! method_exists($this, $action))
77      {
78          throw HTTP_Exception::factory(404,

我确信我已正确设置路线。我没有发现有关Request对象的迁移文档从3.2到3.3的任何更改。或者我错过了什么?

1 个答案:

答案 0 :(得分:0)

默认情况下,请求和响应都在Controller类中初始化(参见下面的代码),因此不需要覆盖它的构造函数。尝试删除你的构造函数,如果这没有帮助,那么你的路由就搞砸了。

abstract class Kohana_Controller {

    /**
     * @var  Request  Request that created the controller
     */
    public $request;

    /**
     * @var  Response The response that will be returned from controller
     */
    public $response;

    /**
     * Creates a new controller instance. Each controller must be constructed
     * with the request object that created it.
     *
     * @param   Request   $request  Request that created the controller
     * @param   Response  $response The request's response
     * @return  void
     */
    public function __construct(Request $request, Response $response)
    {
        // Assign the request to the controller
        $this->request = $request;

        // Assign a response to the controller
        $this->response = $response;
    }