CakePHP custom flash for loginError

女生的网名这么多〃 提交于 2019-12-12 09:23:48

问题


I have an error like:

<?php
    $this->Auth->loginError = "No, you fool!  That's not the right password!";
?>

But I have a custom flash element. How do I use that? and how I pass in extra information like in the below example.

Sample flash:

$this->Session->setFlash('Your settings have been updated', 'flash', array('myclass' => 'success'));

Custom flash element:

<div id="flashMessage" class="message<?php if(isset($myclass)) { echo ' ' . $myclass; } ?>">
 <div class="content">
   <?php if(isset($header)) { echo '<h3>' . $header . '</h3>'; } ?>
   <p><?php echo $message; ?></p>
 </div>
</div>

回答1:


This may be old, but I had a similar problem, and solved it by assigning the layout to the AuthComponent member field flashElement;

$this->Auth->flashElement = 'custom_flash';

See customn auth flash.




回答2:


This works for me:

$this->Auth->flash['element'] = 'flash/auth';

will use te element in

app/View/Elements/flash/auth.ctp

"Authentication — CakePHP Cookbook v2.x documentation"




回答3:


It is 3 years late but here is a complete answer. In you AppController in beforeFilter method use following:

$this->Auth->flash = array(
    'element' => 'flash',
    'key' => 'auth',
    'params' => array(
        'myclass' => 'your auth message class',
        'header' => 'your auth message header',
    )
);

As suggested in in CookBook




回答4:


Many years later, but this might help some. You can set the element in the AppController like this:

class AppController extends Controller {

    public $components = [
        'Auth' => [
            'flash' => ['element' => 'auth'],
        ],
    ];

}



回答5:


the Session->setFlash method writes a message into the $session variable available on the cakephp views. in your element you'd have to do something like this:

<?php echo $session->read('Message.flash.message')?>

Have a look at the setFlash api. You can add extra parameters there.

If you need to see the output, try:

<?php debug($session->read('Message.flash')?>

That will show you the flash array with the message and other associated data which you can then use in your custom handler.



来源:https://stackoverflow.com/questions/5949810/cakephp-custom-flash-for-loginerror

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