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