Flash messages and cache

Hello,

I’ve the following code in my main layout:


if ($msg = Yii::$app->session->getFlash('msg', null, true))

  echo $msg;

So, everytime I pass it, I check if it has a message to be showed.

The messages are set in the controllers.

The problem is: this can’t be cached. So, my main view/layout, can’t be cached (see session_cache_limiter())

This is desirable? A simple message who affects all the page?

I’ve implemented a “cookie flash”, who does the same thing with cache, but it feels wrong.

Tried to set session_cache_limiter to private_no_expire, but it feels like undefined behavior.

When I say cache, I mean the browser cache, HTTP HEADERS. Back button reload the page.

[size="2"]Add to your controller: [/size]

[size="2"]for Yii 1.1[/size]


[

    public function filters()

    {

 		return array(

                array(

                    'CHttpCacheFilter + index',

                    'cacheControl' => 'private, max-age=10800, pre-check=10800',

                    'lastModified' => strtotime('2014-01-01'),

                ),

    	);

    }



for Yii 2




    public function behaviors()

    {

        return [

  	  	[

  		  	'class' => 'yii\filters\HttpCache',

  		  	'only' => ['index'],

  		  	'sessionCacheLimiter' => 'private_no_expire',

  		  	'lastModified' => function($action, $params) {

  			  	return strtotime('2014-01-01');

  		  	},

  	  	]

    	]

    }



Thanks @Radu, nice snippet!

Glad it help you @Phong

Unfortunately for me this doesnt work, even changing the cache variable in php.ini doesnt work.

Only calling session_cache_limiter() before session_start() in code works. Some config is messing with me I guess.

I tested session_cache_limiter(‘private_no_expire’) and the behavior is weird…

BUT, this work like a charm for me -> session_cache_limiter(’’); :D