0

ログイン時にセッション情報にアクセスできません。CakePHP のプラグインである Nice Auth を使用しています。

ここでいくつかの指示に従いました

私がやろうとしているのは、ユーザーのユーザー名にアクセスして、別のページでサポート チケットを送信するときに自分が誰であるかを記入する必要がないようにすることです。

これは、Userscontroller のログイン関数です。

public function login(){
        if ($this->request->is('post') || ($this->request->is('get') && isset($this->request->query['openid_mode']))) {
            if ($this->Auth->login()) {
                $this->redirect($this->Auth->redirect());
                }
            else {
                $this->Session->setFlash(__('Invalid username or password, try again'));
                }
            }
        }

これは、私が置くと仮定する場所です

$this->Session->write('User.id', $userId);

以下はチケットコントローラーです。

public function send()
    {
        $userId = $this->Auth->user();
        if ( !empty($this->request->data) )
        {
            $email = new CakeEmail();
            $email->from(array('xxxx@example.com'))
                //->to($this->request->data['to'])
                ->to(array('helpdesk@example.com'))
                ->subject($this->request->data['subject']);

            if ($email->send($this->request->data['message']))
            {
                $this->Session->setFlash(__('Email Sent Successfully'),
                    'default', array('class' => 'success'));
            }
        }

    }

このコードを配置する場所:

$userId = $this->Session->read('User.id');

そして、私のビューに表示するには:

$userId = $session->read('User.id');

今、セッション コンポーネントとヘルパーをファイルに追加しました。

どんな助けでも大歓迎です。

現在のエラー メッセージは次のようになります。

Notice (8): Undefined variable: session [APP/View/Tickets/send.ctp, line 10] Fatal error: Call to a member function read() on a non-object in /Users/bellis/workspace/cake/app/View/Tickets/send.ctp on line 10
4

3 に答える 3

1
    set session in your $helpers array in your controller or app controller

    var $helpers=array('Session'); 

   if you have use cake 1.2 ver use above code and if you have use higher ver  like 2.0, 2.1 etc use below code

public $helpers = array('Session');




    your can set variable in controller

        $userId = $this->Session->read('User.id');

        $this->set('userid', userId );


        and directly access $userid variable in your ctp file


        OR

        You can directly access

        $userId = $this->Session->read('User.id');

        in your ctp file
于 2012-07-25T04:06:34.613 に答える
0

結局のところ、推奨される方法は次のようにすることです。

$userId = $this->Auth->user('id');

任意のコントローラーからそのメソッドを介して、ログインしているユーザーにアクセスできます。

于 2012-07-25T17:22:04.333 に答える
0

以下を使用して、コントローラーにセッションヘルパーを含めます。

 public $helpers = array('Session',.........);

ビューで次のように使用してみてください。

$userId = $this->Session->read('User.id');

うまくいかない場合は、お気軽にお問い合わせください。

于 2012-07-25T04:03:57.223 に答える