2011-08-30 342 views
1

我使用Kohana 3.2框架。我有一個靜態字符串的變量。 代碼index.php控制器:獲取錯誤:undefined variable;該怎麼辦?

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。 問題在哪裏?

回答

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

不工作。同樣的錯誤。 – reGative

+0

您的視圖中是否存在$ site_name變量? – matino

+0

是的,$ site_name變量在index.php視圖中退出。 – reGative