我认为它不是一个好主意,但方法很简单。
使用的版本:CakePHP的v3.4.5
模板\用户\ login.ctp:
<form method="post" action="<?= $this->Url->build(['controller'=>'Users', 'action' => 'login']) ?>">
<input type="text" name="id" value="">
<button type="submit">login</button>
</form>
控制器\ UsersController.php:
public function login()
{
if ($this->request->is('post')) {
$user = $this->Users->findById($this->request->getData()['id'])->first();
if ($user) {
$this->Auth->setUser($user);
return $this->redirect(['controller' => 'MyController', 'action' => 'MyAction']);
}
$this->Flash->error('Id not found.');
return $this->redirect($this->referer());
}
}
不要忘记在AppController中加载AuthComponent并允许非授权用户使用登录方法 - 描述内容为here。
完美!非常感谢你。 :) – Tom