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 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)