在cakephp2.2中使用auth组件进行基本登录

时间:2012-09-29 07:10:16

标签: cakephp-2.2

你能告诉如何使用cake2.2中的auth组件通过数据库表进行身份验证来登录。 正如我的AppController.php是: `

class AppController extends Controller {

    var $components = array('Auth', 'Session');
    var $helpers = array('Form');




}` 

我的UsersController.php是:

    class UsersController extends AppController {
    var $name = 'Users';
    var $components = array('Auth'); 

    function login() 
    {

    }
    function logout() 
    {
    $this->redirect($this->Auth->logout());
    }
  }

我的观点为:view \ user \ login.ctp

    <?php
    echo $this->Session->flash('auth');
    echo $this->Form->create('User');
    echo $this->Form->input('username');
    echo $this->Form->input('password');
    echo $this->Form->end('Login');
?>

2 个答案:

答案 0 :(得分:1)

public function login() {
    if ($this->Auth->login()) {
        $this->redirect($this->Auth->redirect());
    }
}

答案 1 :(得分:1)

您可以设置Auth配置组件,看起来应该像

public $components = array(
    'Auth' => array(
        'loginAction' => array(
            'controller' => 'users',
            'action' => 'login',
            'plugin' => 'users'
        ),
        'authError' => 'Did you really think you are allowed to see that?',
        'authenticate' => array(
            'Form' => array(
                'fields' => array('username' => 'email')
            )
        )
    )
);

登录功能,有点像Ceeram发布的那样

<?php
public function login() {
    if ($this->request->is('post')) {
        if ($this->Auth->login()) {
            return $this->redirect($this->Auth->redirect());
        } else {
            $this->Session->setFlash(__('Username or password is incorrect'), 'default', array(), 'auth');
        }
    }
}

我所说的一切都在关于Auth的官方文档和本教程中得到了很好的解释http://book.cakephp.org/2.0/en/core-libraries/components/authentication.html