2012-01-01 11 views
0

私はCakePHP 2.0を使用しています。ユーザがアプリケーションにログインしている場合、どのように動的コンテンツを受け取ることができますか?ユーザがログに記録されている場合、別のメニューをエコーする

viewログインまたはログアウトするためのメニューを表示したいのですが、どうすればいいですか?

// I'm in the default template view 
if (!AuthComponent::loggedIn()) { 
    $menu = $this->Html->link('Login', array('controller' => 'users', 'action' => 'login')); 
    $menu .= $this->Html->link('Register', array('controller' => 'users', 'action' => 'register')); 
} else { 
    $menu = $this->Html->link('Home', array('controller' => 'users', 'action' => AuthComponent::user('id'), AuthComponent::user('username'))); 
    $menu .= $this->Html->link('Logout', array('controller' => 'users', 'action' => 'logout')); 
} 
echo $menu; 

私はこれに似ていると思ったが、私はそれを読んだ。それはMVCルールを破る。

CakePHPではどうすればいいですか? オンラインでいくつかの例がありますか?

答えて

1

彼らはそれに応じて、その要素を使用し、コントローラにログインしたりしていない場合は、設定することができ、このようなものを置くことができます。あなたがに、AuthComponentのデータを渡すことができ

echo $this->Html->link('Home', array('controller' => 'users', 'action' => AuthComponent::user('id'), AuthComponent::user('username'))); 
echo $this->Html->link('Logout', array('controller' => 'users', 'action' => 'logout')); 

<?php echo $this->element($userBar); ?> 

が続いmemberBar要素とguestBar要素を持っている:あなたのレイアウトでは

function beforeFilter() { 
    if($this->Auth->loggedIn()) { 
     $userBar = 'memberBar'; 
    } else { 
     $userBar = 'guestBar'; 
    } 
    $this->set('userBar', $userBar); 
} 

:あなたのコントローラで

レイアウト内のオブジェクトの使用を避ける要素。

0

訪問者用とログインユーザー用の異なるレイアウトを使用します。

あなたがapp_controller.phpで

function beforeFilter() { 
     if($this->Auth->user()){ 
      $this->layout = 'members'; 
     } 
    } 
関連する問題