Laravel Redirect::intended() conditional fallbacks

删除回忆录丶 提交于 2019-12-13 06:50:00

问题


To my understanding, Redirect::intended() will redirect to a users intended page prior to logging in, or fall back to a url that can be passed as an argument.

My question is this: How would I make it so that it first checks if there is an intended url in the session, if not it does a Redirect::back() instead, and if that fails it will redirect to the users profile which would be the same as Redirect::route('users.show', Auth::user()->username);


回答1:


Notes:

  1. Partially, the behavior you expected can be attained by passing the referer to Redirect::intended as parameter the referer.

  2. The rest depends on the criteria of a Redirect::back failure as you meant it.

Answer:

Here is my take

// Retrieve the referer - Ripped from Redirect::back()
$back = Redirect::getUrlGenerator()->getRequest()->headers->get('referer');

// your expected Fall back Redirection logic
if (itSucceed($back)) {
    Redirect::intended($back);
} else {
    Redirect::route(...)
}



回答2:


$fallbackUrl = Request::header('referer') ?: URL::route('users.show', Auth::user()->username);

Redirect::intended($fallbackUrl);


来源:https://stackoverflow.com/questions/23967131/laravel-redirectintended-conditional-fallbacks

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!