In ProjectsController.php I'm setting a session variable as I'd like that info to be accessible in ALL controllers, models and views:
$this->Session->write('Project.title', $this->Project->title);
Now, when I try access it from Projects view, like this:
<p>Project: <strong>
<?php if (isset($session->read('Project.title'))):
$session->read('Project.title');
?>
<?php else: ?>
Not selected
<?php endif; ?>
</strong></p>
I'm getting the following error:
Fatal error: Can't use method return value in write context
Which refers to the second line of above code.
I've been through CakePHP documentation and also searched SO, what am I doing wrong here?
Thanks!
EDIT:
I've also tried using:
$this->Session->read('Project.title')
resulting in the same error message.
You should do what PHP tells you once in a while :) The error message is pretty clear. You cannot use isset() and empty() this way. They only work with variables direcly, not methods. So use
<?php if ($this->Session->check('Project.title')) {
echo $this->Session->read('Project.title');
} ?>
as documented in the cookbook
you could also do
<?php
$title = $this->Session->read('Project.title');
if ($title) {
echo $title;
} ?>
or even
<?php if ($title = $this->Session->read('Project.title')) {
echo $title;
} ?>
the last one is not cakephp coding convention, though.
isset()
cannot deal with functions like that. Store the return value of that $session
object's read()
function, then test that variable inside the if()
condition:
$title = $session->read('Project.title');
echo ($title) ? $title : '';