登录时未显示CakePHP 3错误消息
当用户或密码不正确时,我希望在默认情况下显示flash错误消息 这是我在UsersController中的代码:登录时未显示CakePHP 3错误消息,cakephp,cakephp-3.0,Cakephp,Cakephp 3.0,当用户或密码不正确时,我希望在默认情况下显示flash错误消息 这是我在UsersController中的代码: class UsersController extends AppController { public function login() { if($this->request->is('post')) { $user = $this->Auth->identify();
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错误消息(当控制器重定向我时)
此外,闪存卡在项目的任何部分都能完美工作。它只是在登录时不显示
这是我的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>
登录
谢谢您在登录视图中没有使用布局 但是输出flash消息的代码通常位于
默认布局中,您可以在该布局中找到如下内容
<?= $this->Flash->render('auth') ?>
所以你可以做两件事:
把那行代码放在login.ctp中
创建包含该代码的登录布局,并在视图中使用该布局
您在登录视图中没有使用布局
但是输出flash消息的代码通常位于默认布局中,您可以在该布局中找到如下内容
<?= $this->Flash->render('auth') ?>
所以你可以做两件事:
把那行代码放在login.ctp中
创建包含该代码的登录布局,并在视图中使用该布局
必须调用Flash助手的render()方法才能显示Flash成功/错误消息
您可以将此行放入布局文件:
<?= $this->Flash->render() ?>
或
您可以调用特定的ctp文件。您必须调用Flash Helper的render()方法来显示Flash成功/错误消息
您可以将此行放入布局文件:
<?= $this->Flash->render() ?>
或
您可以调用特定的ctp文件