2013-03-03 58 views
0

我正在開發我的第一個CakePHP項目。我以前只在程序PHP中工作過,而且我沒有處理過任何類似Cake框架的問題,所以我遇到了一些問題。我非常感謝幫助我的登錄腳本,這是行不通的。爲什麼我的CakePHP登錄腳本不工作?

我的應用程序將是橄欖球俱樂部的系統。因此,登錄頁面要求用戶從下拉列表中選擇他們的俱樂部,然後輸入密碼。

編輯:

工作如下:

/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'); 
} 
+0

我設法解決它。我不完全確定如何。我會很快發佈經過編輯的工作代碼。 – gazareth 2013-03-04 00:30:25

回答

1

這不是調用名爲login功能,它是一個屬性的存在命名login測試:

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

中的auth組件沒有名爲login的屬性,因此此測試將始終爲假。

如圖所示in the book,你需要調用功能登錄登錄用戶:

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

Cake API說它是一個函數:http://api.cakephp.org/2.3/class-AuthComponent.html#_login – gazareth 2013-03-03 22:18:59

+0

並且說它不需要參數,CakePHP教程中的例子就是這樣的。 – gazareth 2013-03-03 22:20:53

+0

我已經做了好幾次,但我沒有關注。你提出的解決方案是什麼? – gazareth 2013-03-03 22:21:29

相關問題