我一直在到处搜索,但不明白为什么我的Auth::attempt()总是失败。请帮帮我。
这是我的表单
<h2>Admin Login</h2>
<form method="post" action="<?php echo url('login-process') ?>">
Username: <input type="text" name="username" /> <br/>
Password: <input type="password" name="password" />
<button type="submit">Login</button>
</form>
这是我的登录函数:
$username = Input::get('username');
$password = Input::get('password');
if (Auth::attempt(array('username' => $username, 'password' => $password))) {
echo "success!"; //just for testing purposes
} else {
echo "fail!";
My User.php是默认的。如下所示:
use Illuminate'Auth'UserInterface;
use Illuminate'Auth'Reminders'RemindableInterface;
class User extends Eloquent implements UserInterface, RemindableInterface {
/**
* The database table used by the model.
*
* @var string
*/
protected $table = 'users';
/**
* The attributes excluded from the model's JSON form.
*
* @var array
*/
protected $hidden = array('password');
/**
* Get the unique identifier for the user.
*
* @return mixed
*/
public function getAuthIdentifier()
{
return $this->getKey();
}
/**
* Get the password for the user.
*
* @return string
*/
public function getAuthPassword()
{
return $this->password;
}
/**
* Get the e-mail address where password reminders are sent.
*
* @return string
*/
public function getReminderEmail()
{
return $this->email;
}
}
下面是我保存新用户信息的方法(使用Eloquent):
$user = new User();
$user->username = Input::get('username');
$user->email = Input::get('email');
$user->password = Hash::make(Input::get('password'));
$user->save();
但是当用户使用正确的凭据登录时,我一直失败。这几天我一直在做这个,但我总觉得我错过了一些微不足道的东西。请帮个忙好吗?谢谢!
如果你有更多的错误,这将更容易,一个错误消息或其他东西。但下面是我如何登录我的用户。自从最新安装Laravel 4.1.26以来,新的方法给了我错误。
它不在下面的部分,但我认为它的格式更清晰一些。它可能是新记忆令牌的方法。请给出更多的错误信息。你得到一个Laravel错误。您所拥有的回声将不会显示,因为它不会保持该屏幕。也许使用return var_dump('Success')
,这将打破这一进程。
$credentials = [
"username" => Input::get("username"),
"password" => Input::get("password")
];
if(Auth::attempt($credentials)) {
return true;
} else {
return false;
}
在您的User
模型中实现这些方法是一件好事,这是必要的,因为4.1.26:
public function getRememberToken() {
return $this->remember_token;
}
public function setRememberToken($value) {
$this->remember_token = $value;
}
public function getRememberTokenName() {
return 'remember_token';
}