我是CakePHP的新手,我无法验证登录表单。我得到了以下错误:注意(8):未定义变量: user APP/Template\user\login.ctp,第5行
我已经尝试使用以下代码:<?= $this->Form->create('User'); ?>错误被删除,但是验证不起作用。
有人能帮我吗?
login.ctp
<br>
<div class="index large-4 medium-5 large-offset-4 medium-offset-4 columns">
<div class="panel">
<h2 class="text-center">Login</h2>
<?= $this->Form->create($user); ?>
<?php
echo $this->Form->input('email');
echo $this->Form->input('password');
?>
<?= $this->Form->submit('Login', array('class' => 'button')); ?>
<?= $this->Form->end(); ?>
</div>
</div>登录功能- UsersController.php
// Login
public function login()
{
if($this->request->is('post'))
{
$user = $this->Auth->identify();
if($user)
{
$this->Auth->setUser($user);
return $this->redirect(['controller' => 'comentario']);
}
// Erro no Login
$this->Flash->error('Erro de autenticação');
}
}发布于 2016-07-22 18:18:48
首先,更改这一行
<?= $this->Form->create($user); ?>到这个
<?= $this->Flash->render('auth') ?>
<?= $this->Form->create() ?>然后,你可以像这样简化你的提交
<?= $this->Form->button(__('Login')); ?>确保在src/Model/Table中创建UsersTable.php,并将以下代码放入
// src/Model/Table/UsersTable.php
namespace App\Model\Table;
use Cake\ORM\Table;
use Cake\Validation\Validator;
class UsersTable extends Table
{
public function validationDefault(Validator $validator)
{
return $validator
->notEmpty('username', 'A username is required')
->notEmpty('password', 'A password is required')
}
}在登录方法中使用重定向特定控制器是不好的。改变它:
return $this->redirect($this->Auth->redirectUrl());然后告诉您的Auth组件在登录后用户应该重定向在哪里。
$this->loadComponent('Auth', [
'loginRedirect' => [
'controller' => 'Articles',
'action' => 'index'
],
'logoutRedirect' => [
'controller' => 'Pages',
'action' => 'display',
'home'
]
]);也是最重要的。读取认证和授权教程
https://stackoverflow.com/questions/38492123
复制相似问题