douxiongye5779 2012-09-18 17:21
浏览 43
已采纳

无法调用CakePHP setFlash函数

My client inherited an incomplete CMS and I am trying to help them fix it. Keep in mind that I come from front end development, some logic that may appear obvious to some of you totally elude me.

So I managed to almost complete and better the CMS, however there is the issue of logging in. If a user enters the right username and password, all works properly.

However, it would be simple courtesy to indicate to the user when something wrong was done, like "retry to enter your password" or some better phrased message. But nothing happens now.

So I looked into the code, ended up comparing these two files:

login.ctp (the view, as I understand it)

<?php
    echo $this->Session->flash('auth');
?>

<div id="loginColumns">

    <!-- input fields -->
    <div id="loginLeftColumn">
        <?php
            echo $this->Form->create('User',array('action'=>'login'));
            echo $this->Form->input('username');
            echo $this->Form->input('password');
        ?>
    </div>

    <!-- login button -->
    <div id="loginRightColumn">
        <?php echo $this->Form->end('Continue'); ?>
    </div>

</div>

UsersController.php (the controller, as the name implies)

<?php
    class UsersController extends AppController
    {
        var $name = 'Users';
        public $scaffold;


        public function beforeFilter()
        {
            $this -> Auth -> allow(array('login', 'logout'));
        }


        public function login()
        {
            if($this -> request -> is('post'))
            {
                if($this -> Auth -> login())
                {
                    $this -> redirect($this -> Auth -> redirect());
                }
                else
                {
                    // this does not work, maybe because there is no session yet
                    // when users have not logged in yet?
                    $this -> Session -> setFlash(__('Invalid username or password, try again'));
                }
            }
        }


        function logout()
        {
            $this -> redirect($this -> Auth -> logout());
        }
    }
?>

As you can see, there seems to be something that handles erros when logging, but there is nothing showing. That setFlash function is used in a couple of places once the user is logged in, and it works like a charm.

Is there any way for me to use it before the user logs in, or do I have to recreate it from scratch? And if so, what could be some leads into doing it so it looks and feels similar?

Thanks!

  • 写回答

3条回答 默认 最新

  • dongliao8069 2012-09-18 18:10
    关注

    Stick with using this in your view:

    <?php echo $this->Session->flash(); ?>
    

    This will display the Flash message you are setting in the controller.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置