简体   繁体   English

无法使用管理员前缀登录并更改cakephp3中的控制器名称

[英]Not able to login using admin prefix and changing controller name in cakephp3

I added a prefix admin and changed the controller from Userscontroller to LoginController now the localhost:8765/admin works fine but localhost:8765/admin/login/check_login sbut when i submit my form it give me a error Error: The requested address '/admin/login/check-login' was not found on this server where check_login is my auth action 我添加了一个前缀admin,并将控制器从Userscontroller更改为LoginController,现在localhost:8765 / admin可以正常工作,但是localhost:8765 / admin / login / check_login却失败了,但是在我提交表单时却给了我一个错误错误:请求的地址'/在此服务器未找到admin / login / check-login',其中check_login是我的身份验证操作

Router 路由器

Router::prefix('admin', function ($routes) {
$routes->connect('/', ['controller' => 'Login', 'action' => 'display','login']);
});

App Controller 应用控制器

   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']);

Check Login Action 检查登录操作

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;
    }
}

Thanks in advance 提前致谢

Why are you complicating? 你为什么要复杂? The easiest way: 最简单的方法:

  1. Create Admin folder in you Controller app folder. 在Controller app文件夹中创建Admin文件夹。
  2. Inside - create UsersController.php and make login method. 内部-创建UsersController.php并创建登录方法。 All functions inside Admin folder will be using only with Admin prefix in URL. Admin文件夹中的所有功能将仅与URL中的Admin前缀一起使用。

Next - You can map the URL /admin/login to your login() action of users controller using following route: 下一步-您可以使用以下路线将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']);
});

This is the easiest way. 这是最简单的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM