我想使用 CakePHP 3.8 的身份验证插件,但我遇到了文档中没有的问题。
之后按照入门 (https://book.cakephp.org/authentication/1/en/index.html) 我有一个问题。
最初指定 $fields 来更改真实数据库中的用户名和密码关系,与 Auth 组件相同,登录 URL 是加载登录表单的位置。
首先,在入门或文档的任何部分中都没有提到登录视图(表单),因此,就像旧的身份验证组件一样,我将其创建到 Users/login.ctp
<div class="users form">
<?= $this->Flash->render('auth') ?>
<?= $this->Form->create() ?>
<fieldset>
<legend><?= __('Please enter your email and password') ?></legend>
<?= $this->Form->input('email') ?>
<?= $this->Form->input('password') ?>
</fieldset>
<?= $this->Form->button(__('Login')); ?>
<?= $this->Form->end() ?>
</div>
我在 Application.php 中的代码包括这个(及其各自的用途和实现):
public function getAuthenticationService(ServerRequestInterface $request, ResponseInterface $response)
{
$service = new AuthenticationService();
$fields = [
'username' => 'email',
'password' => 'password'
];
// Load identifiers
$service->loadIdentifier('Authentication.Password', compact('fields'));
// Load the authenticators, you want session first
$service->loadAuthenticator('Authentication.Session');
$service->loadAuthenticator('Authentication.Form', [
'fields' => $fields,
'loginUrl' => '/users/login'
]);
return $service;
}
但是当我尝试登录时,我遇到了这个错误,在 login.ctp 中的 var_dump 之后我得到了这个:
object(Authentication\Authenticator\Result)[124]
protected '_status' => string 'FAILURE_OTHER' (length=13)
protected '_data' => null
protected '_errors' =>
array (size=1)
0 => string 'Login URL `http://localhost/users/login` did not match `/users/login`.' (length=70)
如果我评论'loginUrl' => '/users/login'
行,然后登录就可以正常工作了。
补充笔记:
- 我已经使用散列密码和文本平面密码进行了测试,结果相同。
- 我已经添加$this->Authentication->allowUnauthenticated(['view', 'index', 'login', 'add']);
在 beforeFilter 中访问登录。
- 这是一个干净的 cakephp 3.8 安装,只有用于测试的数据库是相同的。
- 我只用蛋糕控制台添加了 Crud。
我想了解有关该 loginURL 的更多信息,我应该在 UsersController 中包含一些用途吗?导致此错误的原因是什么?
谢谢