-
Notifications
You must be signed in to change notification settings - Fork 11.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[5.5] Add Route::redirect() method #19794
[5.5] Add Route::redirect() method #19794
Conversation
Is it useful to pass along the current query string? |
Yes definitely. I did a naive test for this early on but it was hardcoding the query into the destination URL. What do you think about also passing through the headers? Is there a way to create a redirect from an existing request object? |
If we needed a route::redirect method to redirect an URL to another, why don't we use that route directly? I don't think this any useful. |
Because you may need to keep that old route available for backwards compatibility, but a new route is handling the request. I've had to do this many times with applications that have a long enough shelf life. |
|
||
class RedirectController extends Controller | ||
{ | ||
public function __invoke($destination, $status = 301) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing docblocks
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah yes, thanks for pointing this out.
* @param string $url | ||
* @param string $destination | ||
* @param int $status | ||
* @return Route |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@return \Illuminate\Routing\Route
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, I'll amend this after work.
*/ | ||
public function redirect($url, $destination, $status = 301) | ||
{ | ||
return $this->any($url, '\Illuminate\Routing\RedirectController') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using ::class
notation is also fine: return $this->any($url, RedirectController::class)...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I checked a couple other cases and the core and strings seemed to be the convention. I'm happy to use class notation, Taylor can always change it if he's not happy with it.
If we need to keep that old route available for backwards compatibility, but a new route is handling the request, we simply need to define the two routes. If the old route is to be unavailable, just give out the new route. |
@brayniverse what do you mean hard-coding the query into the destination URL? |
I tried |
would it be possible to get this added to 5.4 aswell? |
@rs-sliske you can back-port this functionality using macros. However, if you don't want to bother with that I've got a [route macro package] (https://github.com/brayniverse/laravel-route-macros) which adds this functionality for Laravel 5.1 onwards. |
@brayniverse i realised not long after replying that this wasnt the right solution for my use case, if i need in future i will look into that package (or just use 5.5). thanks :) |
If there are multiple routes with the same uri but different http verbs, this applies to all of them. Any chance of adding a way of specifying the desired http verb? Or perhaps a route name? |
@joaomnb it'd be quite easy to allow an additional parameter to specify the verb - use |
I’ve never done it before, but yeah! Seems like a good chance to start 🙂 Thanks!! |
This adds a new
Route::redirect()
method that handles simple redirects from one route to another. This implementation works with route caching because it uses an internalRedirectController
.The method accepts three arguments
$url
,$destination
, and$status
. The status is optional and defaults to 301.The implementation is based off my package Laravel route macros with one minor difference, the controller makes use of
__invoke
instead of ahandle
method. This is because when writing the test I couldn't find a way to attach the Route to the Request instance, and the__invoke
method is handed each "parameter" - for some reason this works with Laravel's test suite but$request->route('parameter')
does not 🤷♂️ . I can't think of any reason this__invoke
approach will cause any problems.✌️