问题描述
我正在使用简单的API& amp;构建一个 Lumen 应用程序.身份验证.
I'm building a little Lumen application with a simple API & Authentication.
我想将用户重定向到预期的URL,如果他自己访问了/auth/login
,我希望他重定向到/foo
.
I want to redirect the user to the intended URL and if he visits /auth/login
by himself I want him to redirect to /foo
.
Laravel文档中具有以下功能:return redirect()->intended('/foo');
在路由中使用此命令时,服务器日志中显示错误消息:
When I use this in my route I get an error in the server log which says this:
[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
我认为这是因为流明是 Laravel ,也许此功能尚未实现.
I think this is because Lumen is a smaller version of Laravel and maybe this function isn't implemented (yet).
推荐答案
我通过稍微调整中间件以及在会话中存储Request :: path()来解决了这个问题.
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');
}
}
}
在我的route.php中,我有以下路由(我将尽快将其外包给控制器):
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!");
}
});
感谢 IDIR FETT 提出了Request :: path()方法.
希望这将对一些新手有所帮助流明,顺便说一下,这是一个很好的框架. :)
Thanks to IDIR FETT for suggesting the Request::path() method.
Hopefully this will help a few people that are new toLumen, which is a great framework by the way. :)
这篇关于重定向到预期的URL流明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!