user2164884
user2164884

Reputation:

Cakephp 2.0 can't get or read session view file?

I'm working to display or read session in view ctp file, but controller showing is session created and read session is also showing in the controller but can't display or read session in view ctp file?

controller function

var $components = array('Auth','Session','RequestHandler','Email');
$selectedlocation= $_POST['location'];
$this->Session->write('homepagelocation.selectlocation', $selectedlocation);
echo $this->Session->read('homepagelocation.selectlocation');

session reading method in ctp file

echo $this->Session->read('homepagelocation.selectlocation');

Upvotes: 0

Views: 473

Answers (3)

Entom
Entom

Reputation: 29

You can check here: http://book.cakephp.org/2.0/en/core-libraries/helpers/session.html It is possible to use Session read method in view, please set Session as $helper element in Controller.

Upvotes: 0

Vini Antichrist
Vini Antichrist

Reputation: 236

Ello, mate. I think you $_POST[] does not work this way into your controller, you should try:

$this->request->data['location']; //Cake 2.x
$this->data['location']; //Cake 1.3

Then you set up the session to the view:

$this->set('location', $this->Session->read('homepagelocation.selectlocation')); 

Now you can print it on your view:

echo $location;

Upvotes: 0

Colonel Mustard
Colonel Mustard

Reputation: 1533

In order to access data from the controller in your view, you need to set the data to the view.

var $components = array('Auth','Session','RequestHandler','Email');
$selectedlocation= $_POST['location'];
$this->Session->write('homepagelocation.selectlocation', $selectedlocation);
$this->set('location', $this->Session->read('homepagelocation.selectlocation'));

I will ask however, why are you writing data to the session, reading from it and setting that to the view when you already have access to the data you need in $selectedlocation?

Upvotes: 2

Related Questions