问题
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:
Partially, the behavior you expected can be attained by passing the referer to
Redirect::intendedas parameter thereferer.The rest depends on the criteria of a
Redirect::backfailure 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