php - CakePHP 从不同视图登录
问题描述
我正在使用 CakePHP 3.6,我想允许用户从不同的视图登录。我有 2 个相同的表格,但每个页面上都有一个,而另一个只有 1 页。
让我告诉你:
这是我的 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; ?>
这个问题对我来说似乎很明显,但我不知道如何解决它。所有视图中的表单都不起作用,而仅在用户视图中。似乎只有当我在用户视图中时它才有效的登录操作......
谢谢!
解决方案
只需将每个视图中的表单更改为 url,以便将 POST 数据发送到 users/login
echo $this->Form->create(null, [
'class' => 'form-signin',
'url' => ['controller' => 'Users', 'action' => 'login']
]);
推荐阅读
- python - 如何通过 BucketType 定义命令错误消息
- swift - 使用 Podfile post_install 钩子添加自定义构建规则
- ssl - 如何获取 SSL 证书以在 Firefox 上使用 Localhost
- python-3.x - 让雷达条形图列跨越条形的整个宽度 - Matplotlib
- python - 无效输入导致 psycopg2.errors.InvalidTextRepresentation
- java - 如何加载以前的数据并使用按钮继续添加其值(数字/整数)?
- python - Spotipy:当我使用 sp.search() 时出现了错误/几位艺术家
- javascript - 未捕获的类型错误:无法读取未定义的属性“toString”--javascript
- python - 将特定单元格定义为 pandas 数据框中的变量
- c# - 我将如何使用 C# 反序列化分组的 JSON 数据