【发布时间】:2015-01-26 13:57:08
【问题描述】:
我正在关注有关 Laravel 身份验证的教程,以了解更多有关它的信息。我已经到了无法继续前进的地步,因为我很困惑。
问题的继续:我做了一个 Auth::attempt,它可以工作,但是一旦我重定向到一个页面,Auth::get() 就不再工作了。
这是我的控制器:
public function postSignIn()
{
$validator = Validator::make(Input::all(),
array(
'usrUsername' => 'required'
,'usrPassword' => 'required'
));
if($validator->fails()){
return Redirect::route('account-sign-in-get')
->withErrors($validator)
->withInput();
}else{
$auth = Auth::attempt(array(
'usrUsername' => Input::get('usrUsername')
,'password' => Input::get('usrPassword')
,'usrIsActive' => 1
));
if($auth){
return Redirect::intended('/')
->with('global', 'Signed in. Have a great adventure!');
}else{
return Redirect::route('account-sign-in-get')
->with('global', 'Are you sure that is your correct username/password combination?');
}
}
}
这是我在 navigation.blade.php 上的 html 端:
@if(Auth::check())
<li><a href="{{ URL::route('account-logout') }}">Sign Out</a></li>
@else
<li><a href="{{ URL::route('account-create-get') }}">Create account</a></li>
<li class="nav-divider"></li>
<li><a href="{{ URL::route('account-sign-in-get') }}"><i class="glyphicon glyphicon-off"></i> Sign In</a></li>
@endif
登录后进入重定向视图时,可以看到全局消息,但 Auth::get() 始终为 false。我错过了什么?
编辑:我在 if($auth) 之后放置了一个带有 Auth::check() 的 if 条件,它进入了 TRUE 值。所以是的,在重定向时,它似乎丢失了所有的身份验证信息?
Edit2:如有必要,我的用户:
class User extends Eloquent implements UserInterface, RemindableInterface {
use UserTrait, RemindableTrait;
protected $fillable = array('usrEmail', 'usrUsername', 'usrPassword', 'usrCode', 'usrIsActive');
/**
* 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('usrPassword', 'usrCode');
// Tell eloquent the pasword field is "usrPassword"
public function getAuthPassword() {
return $this->usrPassword;
}
}
【问题讨论】:
-
我想你的意思是
Auth::check(),而不是Auth::get()? -
是的,这是正确的 Kestutis。我将编辑文本,这样就可以了。谢谢你。
标签: php session authentication laravel