获取错误:未定义的变量;该怎么办?

时间:2011-08-30 11:14:45

标签: php frameworks kohana

我使用Kohana 3.2框架。我有一个带有静态字符串的变量。 index.php控制器的代码:

 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

模板控制器的代码(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);
  }
}

我的配置文件(common.php

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

我收到错误:ErrorException [ Notice ]: Undefined variable: site_name。 问题在哪里?

1 个答案:

答案 0 :(得分:2)

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

修改 经过第二次看,我觉得你的生活更加艰难。我已经简化了你的代码:

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