出现错误:未定义的变量;该怎么办?

发布于 2024-12-01 20:26:51 字数 1096 浏览 5 评论 0原文

我使用 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 [通知]:未定义变量:site_name。 问题出在哪里?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

风追烟花雨 2024-12-08 20:26:51

尝试:
$this->模板
->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'); 
  }
}

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'); 
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文