我在laravel会话中遇到身份验证问题. (以前在同一系统中工作)
这是我的用户控制器
public function postLogin()
{
$username = Input::get("username");
$password = Input::get("password");
$credentials = array
(
"username"=> $username,
"password" => $password,
);
if (Auth::attempt($credentials))
{
echo Auth::user()->username;
exit();
}
else
{
return Redirect::to("user/login")->with("error_msg","Giriş Başarısız! <br>Lütfen girdiğiniz bilgileri kontrol edip yeniden deneyin.");
}
}
它返回用户名..但是当我重定向页面会话丢失时.
public function postLogin()
{
$username = Input::get("username");
$password = Input::get("password");
$credentials = array
(
"username"=> $username,
"password" => $password,
);
if (Auth::attempt($credentials))
{
return Redirect::to('check_login');
}
else
{
return Redirect::to("user/login")->with("error_msg","Giriş Başarısız! <br>Lütfen girdiğiniz bilgileri kontrol edip yeniden deneyin.");
}
}
和我的路线:
Route::get("check_login",function(){
echo Auth::user()->username;
});
结果:
ErrorException
Trying to get property of non-object
echo Auth::user()->username;
解决方法:
使用数据库会话,更多信息:http://laravel.com/docs/5.0/session#database-sessions
要么
我遇到了同样的问题. &发现我的代码有问题
在我的控制器中:
public function login(Request $request){
//assign $request data to session
Session::put('price',$request->price);
}
public function checkLogin(){
if(login fail){
redirect back to login
}
}
当我重定向到登录会话时覆盖为空,因为当我重定向时,$request为空
所以“价格”为空
所以我在进行会话之前检查会话是否已经有价值
public function login(Request $request){
//check session already got value
if(Session::get('price') == ""){
//assign $request data to session
Session::put('price',$request->price);
}
}
问题为我解决了.
希望对任何有同样问题的人有帮助