dongwenhui8900
2011-01-24 13:51
采纳率: 0%
浏览 61
已采纳

在CakePHP中操作$ session-> flash()视图输出

I am looking to manipulate the $session->flash() output in my CakePHP app, Currently I have the very simple default implementation of showing flash and auth error messages:

<?php
    $session->flash();
    $session->flash('auth');
?>

This produces a <div> with an ID and class that has the message inside. What I would like to do is wrap/replace the generated HTML, specifically with some jQuery UI classes, but wrapping is difficult as I am unable to tell when there is actually a message going to be displayed so I end up with an empty but style error div. What I really need for wrapping to work is to check in $session->flash() returns anything, but I get 'can't use method return value in write context' when checking it with empty();

As far as I can tell the generated HTML is hard coded into the session helper! Bonus points if you can work out how to change the class on the auth message and normal flash message independently.

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • dpdp42233 2011-01-24 15:56
    已采纳

    To check if a message is going to be flashed, put this in the layout

    <?php if($session->check('Message')){ echo $this->Session->flash();} ?>
    

    CSS attributs can be set when you set the message to be flashed

    http://book.cakephp.org/view/1311/Methods#setFlash-1313

    打赏 评论
  • douze2890241475 2011-01-24 14:10

    read up on setFlash() and use the other params that the method takes to define your own elements. you can then do what ever you like.

    http://book.cakephp.org/view/400/setFlash

    打赏 评论
  • dongruan6001 2011-01-24 14:31

    A work-around solution I have found is to set $session->flash() to a variable and then check it with empty() so I can echo out the appropriate <div> if necessary.

    $flashMessage = $this->flash();
    $authMessage = $this->flash('auth');
    

    .. and then check if each one is empty. Of course I have some unnecessary html inside there but as far as the auth message goes, I think this is as flexible as I can get.

    打赏 评论

相关推荐 更多相似问题