问题
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
回答1:
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');
回答2:
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.
回答3:
This is explained on the cakephp website here
来源:https://stackoverflow.com/questions/16827171/cakephp-changing-default-flash-layout