In Cakephp 3, Use another (not users but members)

2019-08-11 16:31发布

问题:

I am using Cakephp 3 for building new application where user have to login for their account however I am hitting the login URL http://localserver.com/members/login and it redirects me to http://localserver.com/users/login

Look like the 'users' controller is set by default in Auth component. How can I override the default controller from 'users' to 'members'?

NOTE: The URLs are not LIVE as I am working on my local-server.

回答1:

Yes, this is related to the userModel config key, which defaults to Users.

Try this script in your controller’s beforeFilter() or initialize() methods.

// Pass settings in
$this->Auth->config('authenticate', [
    'Basic' => ['userModel' => 'Members'],
    'Form' => ['userModel' => 'Members']
]);

Update: In addition to userModel to be worked properly you must set the loginAction too.

// Pass settings in
$this->Auth->config('authenticate', [
    'loginAction' => [
        'controller' => 'Members',
        'action' => 'login',
        'plugin' => false,     // or 'Members' if plugin
    ],
    'Basic' => ['userModel' => 'Members'],
    'Form' => ['userModel' => 'Members']
]);

Cookbook 3.x Doc