laravel 重定向到预期的 URL Lumen

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29963383/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 11:28:26  来源:igfitidea点击:

Redirect to intended URL Lumen

phpredirectlaravellumen

提问by Matthias Wei?

I'm building a little Lumenapplication with a simple API & Authentication.

我正在构建一个带有简单 API 和身份验证的小型Lumen应用程序。

I want to redirect the user to the intended URL and if he visits /auth/loginby himself I want him to redirect to /foo.

我想将用户重定向到预期的 URL,如果他自己访问/auth/login,我希望他重定向到/foo.

In the Laravel Docsthere is this function: return redirect()->intended('/foo');

Laravel Docs 中有这个功能: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

I think this is because Lumenis a smaller version of Laraveland maybe this function isn't implemented (yet).

我认为这是因为LumenLaravel 的一个较小版本,而且这个功能可能还没有实现。

回答by Matthias Wei?

I solved this problem by adjusting my Middleware a little bit as well as storing the Request::path() in the session.

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

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):

在我的 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!");
    }
});

Thanks to IDIR FETTfor suggesting the Request::path() method.
Hopefully this will help a few people that are new to Lumen, which is a great framework by the way. :)

感谢IDIR FETT建议使用 Request::path() 方法。
希望这对一些刚接触 Lumen 的人有所帮助 ,顺便说一下,这是一个很棒的框架。:)

回答by IDIR FETT

i think you have to specify a route name in the intended method, not a URI:

我认为您必须在预期的方法中指定一个路由名称,而不是一个 URI:

return redirect()->intended('foo');

assuming you have already named the route, i think this still works as well:

假设您已经命名了路线,我认为这仍然有效:

return Redirect::intended('/foo');


UPDATE: try this: retrieve the requested URI :

更新:试试这个:检索请求的 URI :

$uri = Request::path(); // Implemented in Lumen

then redirect to the requested URI :

然后重定向到请求的 URI :

return redirect($uri);

this could work !!

这可以工作!

回答by Crembo

Indeed looking at the source code of Lumen it is not implemented: https://github.com/laravel/lumen-framework/blob/5.0/src/Http/Redirector.php

确实查看 Lumen 的源代码它没有实现:https: //github.com/laravel/lumen-framework/blob/5.0/src/Http/Redirector.php

Your options are:

您的选择是:

  1. Check Laravel's (Symfony's?) implementation and put it into your own code
  2. Write completely your own implementation – one super simple way of doing it would be store the request URL in a session, redirect to the login page and when the user successfully logs in retrieve the URL from the session and redirect him
  1. 检查 Laravel 的(Symfony 的?)实现并将其放入您自己的代码中
  2. 完全编写您自己的实现——一种超级简单的方法是将请求 URL 存储在会话中,重定向到登录页面,当用户成功登录时,从会话中检索 URL 并重定向他