如何重定向到Laravel 5.6中的预期URL

时间:2018-12-20 21:18:41

标签: php laravel laravel-5

在Laravel应用程序中,我具有用于特定路由的中间件,在该中间件中,我验证了重定向到url的作用。

public function handle($request, Closure $next)
{
    $isValidated= ....
    if ($isValidated) {
        session()->put('url.intended', URL::full());
        return redirect()->route('routeone')
    }
    return $next($request);
}

在您注册时在该表的store方法中,我通过以下方式进行重定向,但事实证明,当我这样做时,它会将我重定向到domain/img/favicon.png,而我没有执行上一条路由< / p>

public function store(Request $request)
{
    ....
    return redirect()->intended(session('url.intended') ?? '/admin');
}

这是什么问题,或者在中间件重定向之前,如何解决此详细信息以将其重定向到url。 ?

2 个答案:

答案 0 :(得分:0)

尝试使用此代码:

$referrer = $this->request->headers->get('referer');

$url = $referrer ? $this->to($referrer) : $this->getPreviousUrlFromSession();

或直接:

$url = request()->headers->get('referer');

session()->put('url.intended', $url);

答案 1 :(得分:0)

您能不能只是这样做

return back();

从文档https://laravel.com/docs/5.7/redirects#creating-redirects