Redirect::to()'s first parameter is mandatory, it will redirect to the path given by the first parameter. Redirect::intended() will redirect to the previously intended location, no path parameter required (you can provide a default one, though).
When to use Redirect::intended() and redirect::to()
I am not exactly figure out the purpose of this two function-
Redirect::to(); Redirect::intended();
What is difference in both.
Redirect::intended() or $redirect->intended() will redirect the user to where they intended to go.
For example, I tried to view a private page, but I was redirected to login. After I logged in, i'd be redirected to my intended location (the page I was trying to access). You can also add to the intended method, such as another page incase the page the user was intending to go to is no longer available.
Redirect::intended('articles.index');
// Try and redirect them to where they were going, if it fails, redirect them to articles homepage
Redirect::to() or $redirect->to() will simply redirect the user to the page as the argument in the method.
Redirect::to('auth.login');
// Redirect the user to the login page
The difference:
Redirect Intended: redirects the user to where they were originally going
Redirect To: Redirect the user to the page YOU specify them to go.
Take a look at the code at vendor/laravel/framework/src/Illuminate/Routing/Redirector.php.
intended() checks if the session index url.intended exists and redirects to it by default or else redirect to $default='/' which can be overwritten.
I have an alternative to void erros:
<?php
/**
* Undocumented variable
*
* @var $urlGenerator Illuminate\Routing\UrlGenerator
*/
$urlGenerator = app(UrlGenerator::class);
$urlGenerator->previous();
// Current and previous are the same
$currentAreThePrevious = $urlGenerator->previous() == $urlGenerator->current();
$goTo = $currentAreThePrevious ? route(static::IF_AUTH_REDIRECT_TO) : $urlGenerator->previous();
if ($urlGenerator->current() != $goTo) {
return redirect($goTo);
}
return redirect()->route('other.route');
@devtiagofranca Would you please explain this code, what it is doing? What errors are you trying to avoid? Also, static::IF_AUTH_REDIRECT_TO comes up as undefined. Where does one find it? What version of Laravel are you using?
Please or to participate in this conversation.