2012-04-13 5 views
2

単純な変数をビューに渡すことができません。誰かが間違っていることを教えてくれますか?Kohana - 変数をビューに渡す

私は、コントローラのユーザーを持っている:私の見解インサイド

class Controller_User extends Controller_Template_Login { 

    public function action_index() 
    { 
     $this->template = 'user/info'; 
     parent::before(); 
     $user = Auth::instance()->get_user();   
     $this->template->content = View::factory('user/info') 
      ->bind('user', $user) 
      ->bind('message', $message) 
      ->bind('errors', $errors);   

     // if a user is not logged in, redirect to login page 
     if (!$user) 
     { 
      Request::current()->redirect('user/login'); 
     } 
    } 

} 

(ユーザー/情報)私は、ユーザーが定義されていないことを取得します。

私は行方不明ですか?

編集:これは、追加することによって固定することができる注:これが動作しない理由を私は知りたいのは何Controller_Template_Login

 $user = Auth::instance()->get_user(); 
     $this->template->user = $user; 

$user = Auth::instance()->get_user(); 

は次のとおりです。

$user = Auth::instance()->get_user();   

    $this->template->content = View::factory('user/info') 
    ->bind('user', $user) 
    ->bind('message', $message) 
    ->bind('errors', $errors); 

答えて

5

最初のケースでは

$this->template->user = $user; 

あなたは$this->templateuserプロパティに$user変数を割り当てます。

第2の場合には:

$this->template->content = View::factory('user/info') 
    ->bind('user', $user) 
    ->bind('message', $message) 
    ->bind('errors', $errors); 

あなたは$this->template->contentuserプロパティに$user変数をバインドします。

+0

$ this-> template-> content = View :: factory( 'user/info')、ビューのコンテンツはtemplate-> contentにのみ割り当てられますが、正しくは使用されません。 – Tarek

+1

@Tarek:このコードは、 'content'プロパティにビューオブジェクトを割り当てます。テンプレートで '$ content'変数を使うと、レンダリングされます。そうでなければ何も役に立たない – zerkms

関連する問題