я пытаюсь изучить Подлинный модуль Kohana, но метод входа в систему всегда возвращает false.
Контроллер:
<?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 {
}
?>
Вы делаете это неправильно.
Чтобы войти в систему, сделайте что-то вроде этого:
$auth = Auth::instance();
if ($auth->login($_POST['username'], $_POST['password']))
{
echo 'hello, '.$auth->$_POST['username'];
}
else
{
echo 'login failed!';
}
Также раскомментируйте строку модуля auth в начальной загрузке вашего приложения:
'auth' => MODPATH.'auth', // Basic authentication
Я не добавлял правила для пользователя. Для получения дополнительной информации [см. Эту ссылку] [1].
Спасибо, ребята, и прошу прощения за это :)
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');
}
}
}