2011-12-24 88 views
0

讓我試着解釋我想在這裏做什麼。我正在嘗試將Codeigniter 2.x的寵物項目重新寫入Kohana 3.2.x.Kohana Sessions Issue

登錄表單提交後我設置會話數據,我能夠檢索在延長Controller_Site_Template但我的控制器的會話數據創建了一個網站模板控制器(下同)

class Controller_Site_Template extends Controller_Template 
    { 

     public $template  = 'templates/hero'; 

     /** 
     * The before() method is called before your controller action. 
     * In our template controller we override this method so that we can 
     * set up default values. These variables are then available to our 
     * controllers if they need to be modified. 
     */ 
     public function before() 
     { 
      parent::before(); 

     if ($this->auto_render) 
     { 
      // Initialize empty values 
      $this->template->title = ''; 
      $this->template->content = ''; 
      $this->template->session = ''; 

      $this->template->styles = array(); 
      $this->template->footer_scripts = array(); 

      $session = Session::instance(); 
      $this->template->session = $session; 

     } 

     } 

     /** 
     * The after() method is called after your controller action. 
     * In our template controller we override this method so that we can 
     * make any last minute modifications to the template before anything 
     * is rendered. 
     */ 
     public function after() 
     { 
      if ($this->auto_render) 
      { 
       $styles = array(
        'assets/css/style.css' => 'screen',); 


       $footer_scripts = array(
            'assets/js/libs/jquery-1.7.1.min.js', 
        'assets/js/application.js', 
       ); 

       $this->template->styles = array_merge($this->template->styles, $styles); 
       $this->template->footer_scripts = array_merge($this->template->footer_scripts, $footer_scripts); 
      } 

      parent::after(); 
     } 

我無法檢索任何視圖文件中的會話數據。

我能夠得到的視圖文件的會話數據的唯一方法是通過在每個擴展Template_Site_Template控制器的會話數據:

$this->template->content->set_global('session',$this->template->session->as_array()); 

有一種簡單的方法來建立和設置可以在所有控制器,modelc,視圖中使用的template_controller中的會話,而不是在每個單獨的控制器上使用set_global?

我不知道我是否解釋得很好,但我習慣於輕鬆使用Codeigniter的$ this-> session-> userdata();可以在任何控制器,模型和視圖中調用它的功能。

非常感謝您對我所做的任何錯誤輸入。

回答

1

您可以設置或使用下列

View::bind_global('session', $session); 
View::set_global('session', $session); 

綁定全局數據大家的意見。如果你打算進一步改變任何數據一起應用邏輯,然後使用綁定

如果不需要更改數據,請使用設置

編輯:哦,以上只是爲了意見,你希望它跨越整個應用程序。

只需在應用程序中使用Session :: instance() - > set()和Session :: instance() - > get(),而不是在應用程序控制器中分配它。

+0

我只是很習慣Codeigniter和Kohana 3在整個MVC上稍微嚴格一些。謝謝Fady。 – 12Bo 2012-01-14 20:38:55