0

While upgrading my own module to work with latest Kohana (3.3) I found malfunction in my scenario. I use template driven schema in my app (My Controllers extend Controller_Theme). But for AJAX calls I used in version 3.2 separate Controller which extends just Controller. I had to instantiate Request object in this controller for access passed variables via POST or GET in Rquest object. I did it in __construct() method:

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');
        }   
    }
}

In myaction() method I can access posted variables like this. But this does not work anymore in Kohana 3.3. I always get this error:

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,

I am sure I have routes set up correctly. I didn't found any changes in migration document from 3.2 to 3.3 about Request object. Or did I missed something?

matino
  • 17,199
  • 8
  • 49
  • 58
leosek
  • 16
  • 3
  • `$this->request = Request::current();` - why do you need to do this? It should be done automatically by the framework, and I don't think it's guaranteed that `Request::current` is going to be available at this stage (in the constructor). Otherwise, if you really have to do it, try to put it inside `public function before() {}` – laurent Dec 11 '12 at 08:58
  • `__construct()` method - here was the problem. When it stays in my Controller, Request object is not constructed. When I replace it with `before()` method as You mentioned, everything is correct. And Yes, `$this->req = Request::current()` can be omitted. Thanks a lot! – leosek Dec 13 '12 at 13:58

1 Answers1

0

Both request and response are initialised by default in the Controller class (see below code), so there shouldn't be need to override it's constructor. Try to remove your constructor and if that doesn't help, then your routing is messed up.

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;
    }
matino
  • 17,199
  • 8
  • 49
  • 58