hrsetyono
hrsetyono

Reputation: 4464

CakePHP - Changing default Flash layout

I know that I can replace the flash markup by creating something like custom_flash.ctp in Elements folder and call it like:

$this->Session->setFlash('Hello', custom_flash)

But how can I use custom layout when not adding the second parameter?

$this->Session->setFlash('Hello')

I thought I can replace the default by having a file named default.ctp inside Elements folder. But I can't.

I want to keep the code as short as possible. That's why I'm looking a way to do this

Any solution? Thanks

Upvotes: 0

Views: 3868

Answers (3)

zzirGrizz
zzirGrizz

Reputation: 1

This is explained on the cakephp website here

Upvotes: 0

user396716
user396716

Reputation:

Try to create your Component:

class MySessionComponent extends Session {
    public function setFlash($message) {
         return $this->setFlash($message, 'custom_flash');
    }
}

and than in your controller just use:

public $components = array('MySession');
$this->MySession->setFlash('Hello');

Upvotes: 4

hrsetyono
hrsetyono

Reputation: 4464

I found the answer from this question.

We need to add this codes in app/Controller/AppController.php

function beforeRender(){
    if ($this->Session->check('Message.flash')) {
        $flash = $this->Session->read('Message.flash');

        if ($flash['element'] == 'default') {
            $flash['element'] = 'fileNameOfYourCustomFlash';
            $this->Session->write('Message.flash', $flash);
        }
    }
}

It basically add element parameter in flash when it doesn't exist yet.

Upvotes: 3

Related Questions