我正在使用CakePHP 3.6,并且希望允许用户从不同的视图登录。我有2种形式相同,但每页上都有一种,另一种仅一页上。
让我告诉你:
这是我的AppControler初始化函数
public function initialize()
{
parent::initialize();
$this->loadComponent('RequestHandler', [
'enableBeforeRedirect' => false,
]);
$this->loadComponent('Flash');
$this->loadComponent('Auth', [
'authorize'=> 'Controller',
'authenticate' => [
'Form' => [
'fields' => [
'username' => 'email',
'password' => 'password'
]
]
],
'loginAction' => [
'controller' => 'Users',
'action' => 'login'
],
// If unauthorized, return them to page they were just on
'unauthorizedRedirect' => $this->referer()
]);
}
这是我的UsersController登录功能:
public function login()
{
if($this->request->is('post')) {
$user = $this->Auth->identify();
if($user) {
$this->Auth->setUser($user);
$this->Flash->success('You logged succesfully!');
return $this->redirect($this->referer());
}
// Invalid login
$this->Flash->error('Incorrect login');
}
}
这是表格(记住它们是相同的):
<?php if(!$loggedIn): ?>
<?= $this->Form->create(null, ['class' => 'form-signin']) ?>
<h2 class="form-signin-heading">Login</h2>
<?= $this->Form->input('email', ['required' => true, 'class' => 'form-control', 'placeholder' => 'Email', 'label' => false]) ?>
<?= $this->Form->input('password', ['type' => 'password', 'required' => true, 'class' => 'form-control', 'placeholder' => 'Contraseña', 'label' => false]) ?>
<?= $this->Form->input('remember', ['type' => 'checkbox', 'value' => 'remember-me']) ?>
<?= $this->Form->submit('Entrar', ['class' => 'btn btn-lg btn-primary btn-block' ]); ?>
<?= $this->Form->end() ?>
<?php endif; ?>
这个问题对我来说似乎很明显,但是我不知道如何解决。该表单在所有视图中均不起作用,仅在用户视图中有效。似乎只有在“用户”视图中时,登录操作才起作用...
谢谢!
答案 0 :(得分:0)
以每种视图的形式简单地更改为url,以便将POST数据发送给用户/登录
echo $this->Form->create(null, [
'class' => 'form-signin',
'url' => ['controller' => 'Users', 'action' => 'login']
]);