Originally posted in the CakePHP Google Groups
I have a problem with some cakephp (version 1.2.3.8166 1.2.5) code. It
concerns using $this->Session->setFlash in a controller action. Part
of the controller code:
$this->Session->setFlash(__(‘Everything fine.’, true), ‘flashinfo’);
$this->redirect(array(‘controller’=>’users’,'action’=>’index’));
As you can see I use a custom layout for the Flash message called
‘flashinfo’:
<div id=”flashInfoMessage” style=”display:none;”>
<?php echo $content_for_layout; ?>
</div>
<script language=”JavaScript” type=”text/javascript”>
<!–
Flash.show(‘flashInfoMessage’);
//–>
</script>
code for the javascript Flash class:
Flash={
show:function(element){
$(element).visualEffect(‘appear’);
this.timer=setTimeout(‘Flash.hide(“‘+element+’”)’,7000);
},
hide:function(element){
$(element).visualEffect(‘fade’);
clearTimeout(this.timer);
return false;
}
};
What this does is show the flash message for 7 seconds after which it
will be hidden by using the nice scriptaculous effect Fade.
I have two of these layouts, one for informational messages (green
background) and one for errors (red background).
This all works perfectly fine as long as I do not use cake view
caching. But of course I want to have caching in the site for speed.
To render the flash message I placed this into the layout
(default.ctp):
<cake:nocache>
<?php $session->flash(); ?>
</cake:nocache>
With caching turned on and a cached version of the page being rendered
already present in the cache, this generates the following error as
soon as the redirect is performed and the specified URL in the
redirect is being rendered:
Notice (8): Trying to get property of non-object [CORE/cake_1.2.3.8166/
libs/view/helpers/session.php, line 145]
Code | Context
$key = “flash”
$flash = array(
“message” => “Uw contactformulier is verzonden.”,
“layout” => “flashinfo”,
“params” => array()
)
$view = false
} else {
$view =& ClassRegistry::getObject(‘view’);
list($tmpLayout, $tmpVars, $tmpTitle) = array
($view->layout, $view->viewVars, $view->pageTitle);
SessionHelper::flash() – CORE/cake_1.2.3.8166/libs/view/helpers/
session.php, line 145
include – APP/tmp/cache/views/home.php, line 72
View::renderCache() – CORE/cake_1.2.3.8166/libs/view/view.php, line
495
Dispatcher::cached() – CORE/cake_1.2.3.8166/dispatcher.php, line 678
Dispatcher::dispatch() – CORE/cake_1.2.3.8166/dispatcher.php, line 123
[main] – APP/webroot/index.php, line 88
Fatal error: Call to undefined method stdClass::renderLayout() in /usr/
www/htdocs/afvalcontainer/cake_1.2.3.8166/libs/view/helpers/
session.php on line 147
The error does not occur on first time rendering of the page,
everything works as expected in that case. Also when caching is
globally turned off, everything works as expected.
Any ideas of what I am doing wrong ? Or is this a bug ?
Oh my o my… This has cost me hours/days. Maybe it’s easy if you’re a long term CakePHPer. But I just started developing with this framework, also never used anything like an MVC framework before. Ok, I know how to do databases, php, javascript,etc… But this is something else. The basics of CakePHP are quite easy to understand and use. Simple stuff like you read in the tutorials: a user has posts, a post has comments. These are all hasMany and belongTo relations, those just work fine when doing pagination. But then you have something like Posts have Tags. Then you get into the hasAndBelongsToMany (HABTM) arena, where the ‘fun’ begins… With just to tables and doing a findAll or paginate is still not all that complicated. (by the way I’m using nightly builds of the 1.2 version).