我添加了前缀admin,并将控制器从Userscontroller更改为LoginController在localhost:8765/admin工作正常,但本地主机:8765/admin/login/check_logins但当我提交表单时,它会给我一个错误错误:在上面找不到请求的地址"/admin/login/check-login"服务器其中check_login是我的身份验证操作
路由器
Router::prefix('admin', function ($routes) {
$routes->connect('/', ['controller' => 'Login', 'action' => 'display','login']);
});
应用程序控制器
public function initialize() {
$this->loadComponent('Flash');
$this->loadComponent('Auth', [
'authenticate' => [
'Form' => [
'fields' => [
'username' => 'username',
'password' => 'password'
]
]
],
'loginAction' => [
'controller' => 'Login',
'action' => 'check_login',
'prefix' => 'admin'
],
]);
// Allow the display action so our pages controller
// continues to work.
$this->Auth->allow(['display']);
检查登录操作
public function check_login() {
if ($this->request->is('post')) {
$admin = $this->Auth->identify();
if ($admin) {
$this->Auth->setUser($admin);
$result['status'] = 'success';
echo json_encode($result);
}
$result['status'] = 'failure';
$result['message'] = 'Your username or password is incorrect';
echo json_encode($result);
die;
}
}
提前感谢
为什么要复杂化?最简单的方法:
- 在控制器应用程序文件夹中创建
Admin
文件夹 - 内部-创建UsersController.php并创建登录方法。
Admin
文件夹中的所有函数将仅在URL中使用前缀为Admin
的函数
下一步-您可以使用以下路径将URL/admin/login映射到用户控制器的login()操作:
Router::prefix('admin', function ($routes) {
// Because you are in the admin scope,
// you do not need to include the /admin prefix
// or the admin route element.
$routes->connect('/login', ['controller' => 'Users', 'action' => 'login']);
});
这是最简单的方法。