2010-06-09 97 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