开发者

Getting error: undefined variable; what to do?

I use the Kohana 3.2 framework.开发者_如何学运维 I have one variable with a static string. Code for index.php controller:

 class Controller_Index extends Controller_Template {
    public function action_index()
    { 
      $articles_ = Model::factory('index')->do_magic();
      $view_content = View::factory('index/index')->set('query', $articles_);
      $this->response->body(View::factory('template/index')
        ->set('page_title', 'Sākums')                      
        ->set('content', $view_content));             
    }
} // End Welcome

And code for Template controller (template.php):

class Controller_Template extends Kohana_Controller_Template {
  public $template = 'template/index';
  public function before() 
  {
    parent::before();
    $config = Kohana::$config->load('common');
    $this->template
      ->set('site_name', $config->site_name);
  }
}

And my config file (common.php)

return array (
  'site_name' => 'reGative.id.lv',
)

I get the error: ErrorException [ Notice ]: Undefined variable: site_name. Where is the problem?


Try:
$this->template ->set('site_name', $config->get('site_name'));

Edit: After a second look I think you made your life harder. I have simplified your code a little:

class Controller_Index extends Controller_Template {
    public function action_index()
    { 
      $articles_ = Model::factory('index')->do_magic();
      $this->template->page_title = 'Sākums';
      $this->template->content = View::factory('index/index')->set('query', $articles_);                  
    }
} // End Index

class Controller_Template extends Kohana_Controller_Template {  
  public $template = 'template/index';  
  public function before()   
  {  
    parent::before();   
    $this->template->site_name = Kohana::$config->load('common')->get('site_name'); 
  }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜