Reputation:
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
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
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
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