0

你能告诉如何使用 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');
?>
4

2 回答 2

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

于 2012-10-18T11:35:27.883 回答
1
public function login() {
    if ($this->Auth->login()) {
        $this->redirect($this->Auth->redirect());
    }
}
于 2012-10-03T10:55:59.763 回答