如何在CakePHP 2.0.4中使用不同的Auth组件模型


How can I use different model for Auth component in CakePHP 2.0.4?

这看起来很琐碎,但我真的找不到哪里可以更改它。我想使用我的"玩家"模型而不是用户,但每次我进入/players/login时,它都会将我重定向到"缺少控制器"页面,并将更改链接到/users/login。

我试过了:

public $components = array(
    'Session',
    'Auth' => array(
        'authenticate' => array('all' => array('userModel' => 'Player'))
     )
);

function beforeFilter() {
    $this->Auth->authenticate = array('all' => array('userModel' => 'Player'));
}

编辑:已解决

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

在$components数组中有所帮助,我认为:D

我想问题在于您没有提供身份验证系统。您正在提供一些设置,以便在所有将要选择的身份验证系统中使用,但您尚未选择(您必须至少提供一个,如Form、Basic、Digest、ecc.)。

$this->Auth->authenticate = array(
    'all' => array('userModel' => 'Member'),
    'Form',
    'Basic'
);

(或$components阵列中的相同)

您应该这样做

public $components=array(
    'Session',
    'Auth'=>array(
        'authenticate'=>array(
            'Form'=>array(
                'userModel'=>'Player',
             )
        ),
        'loginAction'=>array('controller'=>'Players', 'action'=>'login'),

在控制器中使用此代码:

public $components = array(
    'Auth' => array(
    'loginRedirect' => array(
        'controller' => 'applications',
        'action' => 'index'
    ),
    'logoutRedirect' => array(
        'controller' => 'applications',
        'action' => 'login'
    ),
    'authenticate' => array(
            'Form' => array(
                'passwordHasher' => 'Blowfish',
                'userModel' => 'Application'
            )
        ),      
    )
);

不需要为beforeFilter()函数添加任何代码$组件加载Auth组件。

谢谢Sujay