sradha
sradha

Reputation: 2244

How to get session stored value in ctp(template/view file) of cake php 3.2?

Here i am using below code after login in php file

 echo $get_user_id=$this->Auth->user('id');//getting out put 9

but if i am using this in ctp after login

 <?= $this->Form->input('id', ['type'=>'hidden1','value'=>$get_user_id]); ?>

error is coming.

I have done it in another way but its still not working. After login i have stored id in this way.

                        $session = $this->request->session();
                        $session->write('User_id', $this->Auth->user('id'));
                        echo $session->read('User_id');//9

and try to get this value in this way in ctp file.

 <?= $this->Form->input('id', ['type'=>'hidden1','value'=>$session->read('User.id')]); ?>

but ,its still not coming.Its only coming when i set this value to ctp

$get_user_id = $this->Auth->user('id');
$this->set(compact('get_user_id'));
 <?= $this->Form->input('id', ['type'=>'hidden1','value'=>$get_user_id); ?>//ctp file

Is there any way that i can get the session stored id after login in direct way(without using set.) In cakephp 2X version it is easier to get session stored value in ctp But not in cakephp 3X .Sorry i am new to cakephp 3X .Any suggestion will highly appreciate .Thank you in advance.

Upvotes: 3

Views: 2128

Answers (1)

Mazba Kamal
Mazba Kamal

Reputation: 520

you should use Auth then User

$session = $this->request->session();
$session->read('Auth.User.id')

or

$this->request->session()->read('Auth.User')

more info- http://book.cakephp.org/3.0/en/development/sessions.html#accessing-the-session-object

Upvotes: 4

Related Questions