Can't change 'userModel' using AuthComponent in CakePHP

我的梦境 提交于 2020-01-06 14:58:13

问题


I'va been trying to change the 'userModel' from the default 'user' to 'usuario'. I'va done this before in CakePHP 1.3 but I can't get it to work using the lastest version.

Here's my code (AppController.php):

App::uses('Controller', 'Controller');

class AppController extends Controller {
    public $components = array(
        'Auth' => array(
            'loginError' => "Nombre de usuario o contraseña incorrectos.",
            'authError' => "Debes ingresar con tu cuenta de usuario.",
            'loginRedirect' => array('controller' => 'administrador', 'action' => 'productos'),
            'logoutRedirect' => array('controller' => 'usuarios', 'action' => 'login')
        ),
        'Session',
        'Email'
    );

    public function beforeFilter() {
        $this->Auth->authenticate = array(
            'Basic' => array('userModel' => 'Usuario'),
            'Form' => array('userModel' => 'Usuario')
        );
    }
}

Thanks in advance.

EDIT: The component redirects me to "/users/login" instead of "/usuarios/login" and the login form in "/usuarios/login" doesn't work. It's like I never changed the userModel.


回答1:


Try doing this while initializing the Auth Component

In you AppController:

public $components = array(
'Auth' => array(
        'authenticate' => array(
            'Form' => array(
                'userModel' => 'Usuario',
                'fields' => array(
                    'username' => 'username',
                    'password' => 'password'
                )
            )
        )
    )
);



回答2:


Set custom UserModel is nothing to do with Auth::loginAction which is point to /users/login by default. You can override it in Controller::beforeFilter() callback or Controller::$components array. Hope help.




回答3:


Change you $components array to this:

 public $components = array(
    'Auth' => array(
        'loginError' => "Nombre de usuario o contraseña incorrectos.",
        'authError' => "Debes ingresar con tu cuenta de usuario.",
        'loginRedirect' => array('controller' => 'administrador', 'action' => 'productos'),
        'logoutRedirect' => array('controller' => 'usuarios', 'action' => 'login'),
        'loginAction' => array('controller' => 'usuario','action' => 'login','plugin' => null),
        'authenticate' => array('Form' => array('userModel' => 'Usuario')

        ),
    ),
    'Session',
    'Email'
);



回答4:


Try this code

App::uses('Controller', 'Controller');

class AppController extends Controller {
    public $components = array(
        'Auth' => array(
            'loginError' => "Nombre de usuario o contraseña incorrectos.",
            'authError' => "Debes ingresar con tu cuenta de usuario.",
            'loginAction' => array('controller' => 'aucusers','action' => 'login'),
            'loginRedirect' => array('controller' => 'administrador', 'action' => 'productos'),
            'logoutRedirect' => array('controller' => 'usuarios', 'action' => 'login')
        ),
        'Session',
        'Email'
        );
   }

i hv added this the folowing line

'loginAction' => array('controller' => 'aucusers','action' => 'login'),


来源:https://stackoverflow.com/questions/17980196/cant-change-usermodel-using-authcomponent-in-cakephp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!