为什么我的CakePHP登录脚本不起作用?

时间:2013-03-03 22:00:23

标签: cakephp cakephp-2.3

我正在开发我的第一个CakePHP项目。我以前只使用过程PHP,我没有处理像Cake框架这样的东西,所以我遇到了一些问题。我非常感谢使用我的登录脚本提供的帮助,但是无效。

我的应用程序将成为橄榄球俱乐部的系统。因此,登录页面要求用户从下拉列表中选择他们的俱乐部,然后输入密码。

编辑:

工作如下:

/App/Webroot/View/Clubs/login.ctp

  <div class="clubs form">
<?php echo $this->Session->flash('auth'); ?>
<?php echo $this->Form->create('Club'); ?>
    <fieldset>
        <legend><?php echo __('Please select your club and enter your password'); ?></legend>

        <table>

           <tr> 

                <?php 
                echo $this->Form->input('id', array('label' => 'Club name', 'before' => '<td>', 'after' => '</td>', 'between' => '</td><td>', 'type'=>'select','options'=>$clubs)); 
           ?>
           <tr>
               <?php echo $this->Form->input('password', array( 'before' => '<td>', 'after' => '</td>', 'between' => '</td><td>')); 
           ?>
           </tr>
      </table>
    </fieldset>
<?php echo $this->Form->end(__('Login')); ?>
</div>

/App/Controller/AppController.php

    public $components = array(
     'Session',
        'Auth' => array(
            'authenticate' => array(
                'Form' => array(
                    'userModel' => 'Club',
                    'fields' => array(
                        'username' => 'id',
                        'password' => 'password'
                    )
                )
            ),
            'loginRedirect' => array('controller' => 'clubs', 'action' => 'index'),
            'logoutRedirect' => array('controller' => 'pages', 'action' => 'display', 'home'),
            'loginAction' => array('admin' => false, 'controller' => 'clubs', 'action' => 'login')
        ),
       'DebugKit.Toolbar'
   );

    public function beforeFilter() {
        parent::beforeFilter();
        $this->Auth->allow('logout', 'display');
    }

/App/Controller/ClubsController.php

    public function login() {

    if ($this->request->is('post')) {

        if ($this->Auth->login()) {
            return $this->redirect($this->Auth->redirectUrl());         
        } 
        else {
            $this->Session->setFlash(__('Invalid club or password, try again.', 'default', array(), 'auth'));
            $this->set('clubs', $this->Club->find(
                'list', array(
                    'fields' => array('club_name'),
                    'conditions' => array('status' => '2')
                    )
                  )
            );
        }
    }
    else {
        $this->set('clubs', $this->Club->find(
            'list', array(
                'fields' => array('club_name'),
                'conditions' => array('status' => '2')
                )
            )
        );
    }

}

public function logout() {
    $this->Session->setFlash(__('You have been logged out.'));
    $this->redirect($this->Auth->logout());
}

public function beforeFilter() {
    parent::beforeFilter();
    $this->Auth->allow('add', 'login', 'index', 'logout', 'view');
}

1 个答案:

答案 0 :(得分:1)

这不是调用名为login的函数,它正在测试是否存在名为login的属性:

if ($this->request->is('post')) {
    if ($this->Auth->login) {

auth组件没有名为login的属性,因此该测试将始终为false。

如图in the book所示,您需要调用函数登录来登录用户:

if ($this->request->is('post')) {
    if ($this->Auth->login()) {