下面是我的 Controller 代码...
if(Auth::attempt($userdata)){
//echo 'dfdf';
//exit;
return Redirect::to('mydata');
}
如果我取消注释,它会显示 dfdf
...所以这意味着 Auth 没问题...对吗??这是我的 routes.php 代码...
Route::get('mydata', function()
{
return View::make('mydata');})->before('auth');
即使这样也行得通...如果我从 routes.php 中删除 ->before('auth');
公共(public)函数 doLogin()
{
return Redirect::to('mydata');
exit;
}
这意味着连路由器都可以,对吧?
如果我删除 before('auth');
,路由无需登录就可以工作,这似乎甚至路由都很好,那么 return Redirect::to('mydata ');
为什么它不重定向?即使在身份验证后它仍然停留在登录页面上...
最佳答案
评论后编辑:
在 Controller 中,将重定向更改为 ::intended
:
if(Auth::attempt($userdata)){
return Redirect::intended('mydata');
}
来自docs :
The
Redirect::intended
function will redirect the user to the URL they were trying to access before being caught by the authentication filter. A fallback URI may be given to this method in case the intended destination is not available.
API:
intended(string $default = '/', int $status = 302, array $headers = array(), bool $secure = null)
而且您不需要将 ->before('auth')
添加到您的 View
调用中,因为您正在重定向到 mydata
仅 Auth::attempt
返回true
。
因此,请将您的路线更改为:评论后编辑 感谢 Juan Antonio Orozco 指出此不足!!
Route::get('mydata', array('before' => 'auth', function() {
return View::make('mydata');
});
顺便说一句,在 return
之后不需要添加 exit
,因为解释器已经使用 return
命令离开了函数, 所以它甚至不会被执行。
关于php - Laravel Auth 不工作,一切似乎都是正确的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23930604/