phpkohanakohana-3.3

Why is my Kohana 3.3 controller response NULL when called from within another controller?


Why does a controller of mine work ok when called directly but does not respond (though the action code seems to be executed ok) when called from within another controller? Below is the code example.


// This contoller works ok when called directly
// with a browser but its response is null when
// it is called from within another controller
// as it is meant to be (though the action
// code seems to be executed ok in both cases)
class Controller_Bar extends Controller_Template {

    public $template = 'bar';

    public function action_index()
    {
        $items = ... // this gets populated with ORM

        $this->template->items = $items;
    }
}


// This contoller is meant to be called directly
// with a browser and seems to work ok itself
// but the controller it calls inside responds
// with NULL in this case
class Controller_Foo extends Controller_Template {

    public $template = 'foo';

    public function action_index()
    {

        // I expected $barResponse to become a rendered 'bar' view
        // which is meant to be a segment to be merged into the
        // 'foo' view which becomes the resulting web page
        $barResponse = Request::factory('bar')->execute()->response;

        // but this writes NULL to the log
        Log::instance()->add(Log::NOTICE, gettype($barResponse) );

        $this->template->yoo = 'zzz'; // this works ok

        $this->template->bar = $barResponse;
    }
}

Solution

  • Request::execute() returns a Response object. You probably followed an outdated tutorial, $request->response is how it worked in a previous (3.1?) minor version.

    This should work:

    $response = Request::factory('bar')->execute();
    
    $this->template->bar = $response->body();