我正在使用一个简单的API&amp ;;构建一个小Lumen应用程序。认证
我想将用户重定向到预期的网址,如果他自己访问/auth/login
,我希望他重定向到/foo
。
在Laravel Docs中有以下功能:return redirect()->intended('/foo');
当我在我的路线中使用它时,我在服务器日志中收到一条错误,上面写着:
[30-Apr-2015 08:39:47 UTC] PHP Fatal error: Call to undefined method Laravel\Lumen\Http\Redirector::intended() in ~/Sites/lumen-test/app/Http/routes.php on line 16
答案 0 :(得分:5)
I solved this problem by adjusting my Middleware a little bit as well as storing the Request::path() in the session.
This is how my Middleware looks:
class AuthMiddleware {
public function handle($request, Closure $next) {
if(Auth::check()){
return $next($request);
} else {
session(['path' => Request::path()]);
return redirect('/auth/login');
}
}
}
And in my routes.php I have this route (which I will outsource to a controller asap):
$app->post('/auth/login', function(Request $request) {
if (Auth::attempt($request->only('username', 'password'))){
if($path = session('path')){
return redirect($path);
} else {
return redirect('/messages');
}
} else {
return redirect()->back()->with("error", "Login failed!");
}
});
Thanks to IDIR FETT中发布变量以建议Request :: path()方法。
希望这将有助于一些新手
Lumen,顺便说一句,这是一个很棒的框架。 :)
答案 1 :(得分:2)
我认为您必须在目标方法中指定路由名称,而不是URI:
return redirect()->intended('foo');
假设您已经命名了路线,我认为这仍然有效:
return Redirect::intended('/foo');
更新: 试试这个: 检索请求的URI:
$uri = Request::path(); // Implemented in Lumen
然后重定向到请求的URI:
return redirect($uri);
这可能有用!!
答案 2 :(得分:2)
确实在查看Lumen的源代码时,它没有实现: https://github.com/laravel/lumen-framework/blob/5.0/src/Http/Redirector.php
您的选择是: