CakePHP - Changing default Flash layout

老子叫甜甜 提交于 2019-12-12 04:48:02

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!