重定向到预期的URL流明

Mat*_*eiß 1 php redirect laravel lumen

我正在使用简单的API和身份验证构建一个小的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
Run Code Online (Sandbox Code Playgroud)

我认为这是因为LumenLaravel的较小版本,也许这个功能尚未实现.

Mat*_*eiß 5

我通过稍微调整我的中间件以及在会话中存储Request :: path()来解决这个问题.

这就是我的中间件看起来的样子:

class AuthMiddleware {

    public function handle($request, Closure $next) {
        if(Auth::check()){
            return $next($request);
        } else {
            session(['path' => Request::path()]);
            return redirect('/auth/login');
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

在我的routes.php中我有这条路线(我将尽快外包给控制器)​​:

$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!");
    }
});
Run Code Online (Sandbox Code Playgroud)

感谢IDIR FETT建议使用Request :: path()方法.
希望这将有助于一些对Lumen不熟悉的人, 顺便说一句,这是一个很好的框架.:)