2011-12-24 17 views
0

私がここで何をしたいか説明しようとしましょう。私はCodeigniter 2.xからKohana 3.2.xにペットプロジェクトを書き直そうとしています。Kohanaセッションの問題

私は、ログインフォームが提出された後、私はセッションデータを設定し、私は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()); 

が確立し、設定する簡単な方法はありますそれぞれのコントローラーでset_globalを使用するのではなく、すべてのコントローラー、modelc、ビューで使用できるtemplate_controllerのセッション?

私はこれをうまく説明しているのかどうかわかりませんが、私は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全体でちょっと厳しいです。ありがとうございます。 – 12Bo