登录时没有显示CakePHP 3错误消息

时间:2017-10-25 22:36:45

标签: cakephp cakephp-3.0

我想在用户或通行证不正确时默认显示Flash错误消息。

这是我在UsersController中的代码:

class UsersController extends AppController
{
    public function login()
    {
        if($this->request->is('post'))
        {
            $user = $this->Auth->identify();
            if($user)
            {               
              $this->Auth->setUser($user);
              return $this->redirect($this->Auth->redirectUrl());
            }
            else{
                $this->Flash->error(__('user/password incorrect'));
            }   
        }
    }
    ...
}

错误地放置一些字段后,不会显示flash消息。但是,在那之后,如果我正确地输入用户和密码,我可以在下面的视图中看到flash错误消息(当控制器重定向我时)。

此外,闪存卡在项目的任何部分都能完美运行。当它没有出现时,它就在登录中。

这是我的login.ctp

    <?php

use Cake\Cache\Cache;
use Cake\Core\Configure;
use Cake\Datasource\ConnectionManager;
use Cake\Error\Debugger;
use Cake\Network\Exception\NotFoundException;

$this->layout = false;


$cakeDescription = 'Welcome';
?>
<!DOCTYPE html>
<html>
<head>
    <?= $this->Html->charset() ?>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>
        <?= $cakeDescription ?>
    </title>
    <?= $this->Html->meta('icon') ?>
    <?= $this->Html->css('base.css') ?>
    <?= $this->Html->css('cake.css') ?>

</head>
<body class="home">
<br>
<br>
<div class="index large-4 medium-4 large-offset-4 medium-offset-4 columns">
    <div class="panel">
        <h2 class="text-center">Login</h2>
        <?= $this->Form->create(); ?>
            <?= $this->Form->input('email', array('label' => 'write your email')); ?>
            <?= $this->Form->input('password', array('label' => 'write your password')); ?>
            <?= $this->Form->button('Login', ['type' => 'submit']); ?>
        <?= $this->Form->end(); ?>
    </div>
</div>
</body>
</html>

由于

2 个答案:

答案 0 :(得分:2)

您必须调用Flash Helper的render()方法才能显示Flash成功/错误消息。

您可以将此行放在布局文件中:

<?= $this->Flash->render() ?>

或者

你可以特别调用ctp文件。

答案 1 :(得分:0)

您在登录视图中没有使用布局

但输出flash消息的代码通常位于default布局中,您可以在其中找到类似

的内容
<?= $this->Flash->render('auth') ?>

所以你可以做两件事:

  1. 将这行代码放入login.ctp
  2. 创建包含该代码并在视图中使用该布局的登录布局