doushan1863 2011-08-30 11:14
浏览 32
已采纳

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

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?

  • 写回答

1条回答 默认 最新

  • dongweng6241 2011-08-30 11:47
    关注

    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'); 
      }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题