2012-01-10 12 views
0

私はCakePHP初心者です。CakePHP:AuthとSessionを使ってユーザーのグループIDを取得できません。

私は、次のログインコードを持っている:

function login() { 
    if ($this->Session->read('Auth.User')) { 

     $result = $this->User->find('first', array(
      'conditions' => array(
       'User.id' => $this->Auth->user('id') 
      ), 
      'fields' => 'Group.id' 
     )); 
     $groupId = $result['Group']['id']; 
     $this->Session->write('Auth.Group.id', $groupId); 
     $this->Session->setFlash('You are logged in with Group ID ' . $groupId . '!', 'auth'); 
    } 
} 

あなたが見ることができるように、私はAuth.Group.idセッション変数を設定するだけでなく、auth flashています。私app/views/layouts/default.ctpでしかし

、I

<?php echo $session->flash() ?> 

または

<?php echo $session->flash('auth') ?> 

または

<?php echo 'ID=' . $session->read('Auth.Group.id'); ?> 

が、私は何も得ます。私はここで間違って何をしていますか?

編集:ここに私のAppControllerがある

class AppController extends Controller { 
    var $components = array('Acl', 'Session', 'Auth'); 

    function beforeFilter() { 
     //Configure AuthComponent 
     $this->Auth->actionPath = 'controllers/'; 
     $this->Auth->authorize = 'actions'; 
     $this->Auth->loginAction = array('controller' => 'users', 'action' => 'login'); 
     $this->Auth->logoutRedirect = array('controller' => 'users', 'action' => 'login'); 
     $this->Auth->fields = array('username' => 'email', 'password' => 'password'); 
     $this->Auth->allowedActions = array('display','logout','login'); 
    } 

答えて

1

あなたのモデルで収容可能行動を使用していますか?関係なく、あなたはそれがユーザーのために、あなたのgroup_idのあるべき数を、表示されている場合は、私に教えてください

}

function login() { 
if ($this->Session->read('Auth.User')) { 

    $result = $this->User->find('first', array(
     'conditions' => array(
      'User.id' => $this->Auth->user('id') 
     ), 
     'fields' => 'User.group_id' //CHANGE THIS 
    )); 
    $groupId = $result['User']['group_id']; //CHANGE THIS 
    $this->Session->write('Auth.Group.id', $groupId); 
    $this->Session->setFlash('You are logged in with Group ID ' . $groupId . '!', 'auth'); 
} 

...次あたりにあなたのコードを変更しようとすると何が起こるか見なければなりません。そうであれば、それがあなたが探しているものなら、Containable Behaviorを使ってグループ名を得ることができます。

0

あなたが2に設定デバッグレベルを持っていますか? (core.php内)。

setFlashの2番目のパラメータは、メッセージを表示するために使用される要素の名前です(app/views/elements/auth.ctp)。行方不明の場合は、何も手に入らないのが普通です。

は、コントローラでは、以下の

をお試しください:

$this->Session->setFlash('You are logged in with Group ID ' . $groupId . '!'); // notice 2nd param removed 

とレイアウトで:

<?php echo $session->flash() ?> 
+0

まだナダルです。 AppControllerで編集した投稿、多分問題はそこにありますか? – Garrett

関連する問題