2010-06-09 127 views
1

我想學習Kohana的驗證模塊,但登錄方法總是返回false。Kohana 3:驗證模塊

控制器:

<?php defined('SYSPATH') OR die('No Direct Script Access'); 
class Controller_Auth extends Controller { 
    public function action_index() { 
     if($_POST) { 
      $this->login(); 
     } 

     $this->template = View::factory('login'); 
     echo $this->template; 
    } 

    private function login() { 
    $user = ORM::factory('user'); 

     $data = array('username' => 'wilson', 'password' => '123'); 
     if(!$user->login($data)) { 
      echo 'FAILED!'; 
     } 
    } 

    private function logout() { 

    } 
}  
?> 

型號:

<?php defined('SYSPATH') or die('No direct script access.'); 
class Model_User extends Model_Auth_User { 
} 
?> 
+0

請提供您的所有文件。將會更容易理解。 – B4NZ41 2011-09-21 20:20:41

回答

1

你就錯了。

登陸用戶,做這樣的事情:

$auth = Auth::instance(); 
if ($auth->login($_POST['username'], $_POST['password'])) 
{ 
     echo 'hello, '.$auth->$_POST['username']; 
} 
else 
{ 
     echo 'login failed!'; 
} 

此外,取消了身份驗證模塊線在你的應用程序的引導:

'auth'  => MODPATH.'auth',  // Basic authentication 
1

我沒有規則添加到用戶。有關更多信息,請參見[鏈接] [1]。

謝謝你們和我們對此深感抱歉:)

http://webcache.googleusercontent.com/search?q=cache:kXKFWswjDogJ:kerkness.ca/kowiki/doku.php%3Fid%3Dusing_the_auth_module_in_your_controllers+&cd=1&hl=en&ct=clnk&gl=us

public function action_signin() 
{ 
    #If user already signed-in 
    if(Auth::instance()->logged_in()!= 0){ 
     #redirect to the user account 
     Request::instance()->redirect('account/myaccount');  
    } 

    $content = $this->template->content = View::factory('signin'); 

    #If there is a post and $_POST is not empty 
    if ($_POST) 
    { 
     #Instantiate a new user 
     $user = ORM::factory('user'); 

     #Check Auth 
     $status = $user->login($_POST); 

     #If the post data validates using the rules setup in the user model 
     if ($status) 
     {  
      #redirect to the user account 
      Request::instance()->redirect('account/myaccount'); 
     }else 
     { 
          #Get errors for display in view 
      $content->errors = $_POST->errors('signin'); 
     } 

    } 
} 
+0

刪除了我的倒票,因爲我找到了Google緩存副本並進行了修改。 – William 2012-10-11 08:49:12