Controller_Template gives Creating default object from empty value
I'm building a KO3 website and I'm creating a new Controller_Template that looks like this:
<?php
defined('SYSPATH') or die('No direct script access.');
class Controller_Menu extends Contro开发者_StackOverflow社区ller_Template {
public $template = "menu/menu";
public function __construct() {
$this->User = \Base\User::getLoggedInUser();
}
public function action_show() {
// Do totally nothing
}
}
No rocket science in here. In my APPPATH."/views/menu" directory I have a "menu.php" file that looks like this:
<?php
echo "foobar";
Impressive right? ;)
When I try to load http://localhost/menu/show
I get the following error:
ErrorException [ Strict ]: Creating default object from empty value
SYSPATH/classes/kohana/controller/template.php [ 44 ]
39 */
40 public function after()
41 {
42 if ($this->auto_render === TRUE)
43 {
44 $this->request->response = $this->template;
45 }
46
47 return parent::after();
48 }
49
SYSPATH/classes/kohana/controller/template.php [ 44 ] » Kohana_Core::error_handler(arguments)
{PHP internal call} » Kohana_Controller_Template->after()
SYSPATH/classes/kohana/request.php [ 1115 ] » ReflectionMethod->invoke(arguments)
APPPATH/bootstrap.php [ 129 ] » Kohana_Request->execute()
DOCROOT/index.php [ 103 ] » require(arguments)
I have similar function implemented in other controllers and they all work perfectly. So can anybody tell me what is the problem here?
EDIT I dumped the request object in the after method and it is not set, i.e. it has the value null.
I found the solution. I had to call the parent constructor in my overwritten constructor. Changing the code to:
class Controller_Menu extends Controller_Template {
public $template = "menu/menu";
public function before() {
$this->User = \Base\User::getLoggedInUser();
}
public function action_show() {
// Do totally nothing
}
}
Fixed my problem.
EDIT
Changed the code to suggestions in the comments.
精彩评论