所以我想做一個非常基本的身份驗證,並通過遵循https://book.cakephp.org/3.0/en/tutorials-and-examples/blog-auth-example/auth.html上的博客教程,我試圖讓它只使用用戶名登錄,但登錄系統不起作用因爲如何僅使用用戶名進行CakePHP3登錄
$user = $this->Auth->identify();
總是返回false,我已經搜索了關於Authentication的Cake文檔,但找不到任何東西來幫助我。 有人可以告訴我如何做一個costum登錄身份驗證。
當前登錄操作:
public function login()
{
if ($this->request->is('post')) {
$user = $this->Auth->identify();
if ($user) {
$this->Auth->setUser($user);
return $this->redirect($this->Auth->redirectUrl());
}
$this->Flash->error(__('Invalid username, try again'));
}
}
而其景觀:
<div class="users form">
<?= $this->Flash->render() ?>
<?= $this->Form->create() ?>
<fieldset>
<legend><?= __('Please enter your username') ?></legend>
<?= $this->Form->control('username') ?>
</fieldset>
<?= $this->Form->button(__('Login')); ?>
<?= $this->Form->end() ?>
</div>
AuthComponent初始化:
public function initialize(array $config)
{
$controller = $this->_registry->getController();
$this->eventManager($controller->eventManager());
$this->response =& $controller->response;
$this->session = $controller->request->session();
}
您可以查看手動記錄用戶的身份。您可以通過檢查用戶名是否存在以及是否通過獲取用戶信息並使用setUser()方法手動記錄它們來執行此操作。 https://book.cakephp.org/3.0/en/controllers/components/authentication.html#manually-logging-users-in – spencdev