Laravel Redirect::intended() conditional fallbacks

2019-08-19 03:46发布

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

2条回答
唯我独甜
2楼-- · 2019-08-19 04:22
$fallbackUrl = Request::header('referer') ?: URL::route('users.show', Auth::user()->username);

Redirect::intended($fallbackUrl);
查看更多
▲ chillily
3楼-- · 2019-08-19 04:36

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(...)
}
查看更多
登录 后发表回答