2014-04-05 49 views
1

這個函數有問題。目前,我已經設置了我的網站,因此只有在用戶登錄後才能訪問What's On和Offers頁面。如果他們單擊這些頁面,他們將被重定向到登錄頁面。CakePHP Auth-> login()函數不起作用

不幸的是,當用戶登錄時,登錄頁面只是刷新而不是簽入並將其重定向到索引頁面。

我的用戶表具有以下字段:user_id,名,姓,電子郵件,密碼,創建,修改。

這是我的登錄功能,該功能在UsersController代碼:

public function login() { 

    if ($this->request->is('post')) { 
     if ($this->Auth->login()) { 
      return $this->redirect($this->Auth->redirect()); 
     } 
     $this->Session->setFlash(__('Invalid username or password, try again')); 
    } 

這是我的AppController的代碼:

App::uses('Controller', 'Controller'); 

class AppController extends Controller { 


     public $helpers = array(
       'Session', 
       'Html' => array('className' => 'BoostCake.BoostCakeHtml'), 
       'Form' => array('className' => 'BoostCake.BoostCakeForm'), 
       'Paginator' => array('className' => 'BoostCake.BoostCakePaginator'), 
     ); 

    // CakePHP documentation tutorial 
    public $components = array(
     'Session', 
     'DebugKit.Toolbar', 
     'Auth' => array(
      'loginRedirect' => array(
       'controller' => 'pages', 
       'action' => 'index' 
      ), 
      'logoutRedirect' => array(
       'controller' => 'pages', 
       'action' => 'display', 
       'home' 
      ), 
      'flash' => array(
       'element' => 'alert', 
       'key' => 'auth', 
       'params' => array(
        'plugin' => 'BoostCake', 
        'class' => 'alert-error' 
       ) 
      ) 
     ) 
    ); 

    public function beforeFilter() { 
     $this->Auth->allow('index', 'contact'); 
    } 

} 

這是我的代碼爲login.ctp:

<!-- Sign in Form which is on the left hand side of the screen --> 
     <div class="col-xs-6"> 
      <?php echo $this->Form->create('User', array(
      'inputDefaults' => array(
       'action' => 'login', 
       'div' => 'form-group', 
       'label' => array(
        'class' => 'col col-md-3 control-label' 
        ), 
       'wrapInput' => 'col col-md-9', 
       'class' => 'form-control' 
       ), 
       'class' => 'well form-horizontal' 
      )); ?> 

      <?php echo $this->Form->input('email', array(
       'placeholder' => 'Email' 
      )); ?> 
      <?php echo $this->Form->input('password', array(
       'placeholder' => 'Password' 
      )); ?> 

      <!-- Sign in button for the sign in form --> 
      <div class="form-group"> 
       <?php echo $this->Form->submit('Sign in', array(
        'div' => 'col col-md-9 col-md-offset-3', 
        'class' => 'btn btn-default' 
       )); 
       echo $this->Form->end(); ?> 
      </div> 

      <div class="row"> 
      <h5 small><?php echo $this->Html->link("Not registed? Click here to register!", '/users/register') ?></h5> 
      </div> 

希望有人能指出出了什麼問題

回答

0

您使用的是哪個版本?嘗試在這部分使用的redirectUrl而不是重定向(),因爲你應該版本後使用2.3:

return $this->redirect($this->Auth->redirect()); 

如果沒有幫助,嘗試把一些回聲的,看看是否$這個 - > Auth->登錄( )是真的,$ this-> Auth-> redirect()的值是多少。如果一切看起來都正常,試着看看這個flash消息是否真的被你的視圖輸出(如果auth-> login是false的話)。

代碼看起來很好,所以你真的需要調試一些步驟來實際解決問題。

+0

使用CakePHP 2.4.6,現在將嘗試了這一點! –

+0

這也不起作用,您如何在CakePHP中進行調試? –

+0

簡單地說,像echo $ this-> Auth-> redirect()或print_r($ this-> Auth-> redirect());然後是出口;甚至像回聲「我現在在這裏」;會給你一些見解腳本的見解。 –

0

這會幫助你。請在這裏閱讀更多Configuring Authentication handlers

public $components = array(
    'Auth' => array(
     'authenticate' => array(
      'Form' => array(
       'fields' => array('username' => 'email') 
      ) 
     ) 
    ) 
); 
+0

什麼是「用戶名」和「電子郵件」變量應該是什麼? '用戶名'是模型嗎? –

+0

通常情況下,您使用用戶名和密碼進行身份驗證,在您想要使用電子郵件進行身份驗證的門戶中,因此您需要將身份驗證字段從用戶名更改爲電子郵件。 – walkingRed

+0

所以我的應該是'email'=>'密碼'是嗎?還是我誤會了? –

0

它,因爲你忘了什麼東西在你的組件設置 - 添加你的公共$組件必須看起來像這 -

public $components = array(
    'Session', 
    'DebugKit.Toolbar', 
    'Auth' => array(
     'loginRedirect' => array(
      'controller' => 'pages', 
      'action' => 'index' 
     ), 
     'logoutRedirect' => array(
      'controller' => 'pages', 
      'action' => 'display', 
      'home' 
     ), 
     'authenticate' => array(
      'Form' => array(
       'fields' => array(
        'username' => 'email' 
       ) 
      ) 
     ), 
     'flash' => array(
      'element' => 'alert', 
      'key' => 'auth', 
      'params' => array(
       'plugin' => 'BoostCake', 
       'class' => 'alert-error' 
      ) 
     ) 
    ) 
); 
+0

不幸的是,這並沒有工作:( –

+0

你定義'public $ primaryKey'在User.php模型? –

0

你是不是可以發佈您的數據到控制器和行動。試試這個上創建:

<?php echo $this->Form->create('User', 
         array('url' => array('controller' => 'users', 'action' => 'login'), 
         'inputDefaults' => array(
          'div' => 'form-group', 
          'label' => array(
           'class' => 'col col-md-3 control-label' 
           ), 
          'wrapInput' => 'col col-md-9', 
          'class' => 'form-control' 
          ), 
          'class' => 'well form-horizontal' 
         )); ?> 

分配一個登錄按鈕,它更多的用戶frienly

echo $this->Form->end(__('Login')); 

,你可能會發現這個採用全 CakePHP: Cant do Simple Login