-
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
Introduce Http status code response helper #53691
Conversation
During my livestream, I did do benchmarks on this as I was curious the anonymous class. However, the difference between the fluent API and passing an integer were negligible (~.002ms difference). Memory was also negligible. PHP has heavily optimized anonymous classes by storing them in memory. So it is not "rebuilt" on every call. |
There are not enough doc-blocks to let the IDE know which hints to output in the fluent method. |
Why would you need a helper function with fluent API if self-explanatory constants are already available, readable and 100% supported by IDE? I personally don't see much value in this. But even if we consider it as a shortcut for creating an empty response, then:
|
The Response class has a ton of HTTP status codes defined as constants which you can use.
|
I think we can already almost do the integer version via @jasonmccreary this and the #53663 would've been good applications for an enum. @davorminchorov doing that would return a response with body |
@tontonsb, yes, underneath it uses Enums would be nice, but present the same challenge of fundamentally altering how the framework handles return values. As such, that would need to be targeted at 12.x. |
@jasonmccreary I'm not sure about needing to target 12.x. If you create a new enum and add a case among these containing something like |
@tontonsb, I toyed around with this idea and don't think that's the only path. But test it out and open a PR if you like that approach better. 👍 |
I get you want a shorthand but this is just shortening As for the fluent methods (which I absolutely like the idea of btw), IMO it makes more sense to add to the response helper, like Just my 2 cents. |
@x7ryan, yeah, that would be another approach. We'll see where this lands... |
Even though, I'd name the enum
This could also be done gradually in two PRs: One introducing the enum itself in 11.x as "free to use by devs" and one using it in the framework itself in 12.x as @tontonsb suggested
If the number handling of #53663 was to be reverted in favor of this PR, this still wouldn't be perfect, yet a huge improvement 👍🏻
Good catch! That's weird tbh. I would have expected this to be part of a trait/concern among other known status code methods 🤔 What's even weirder, is that its status code can be changed to any other number: framework/src/Illuminate/Routing/ResponseFactory.php Lines 61 to 71 in 8ffd13c
|
I think it's fair to add few words on why enums (that I brought up earlier myself) might not be appropriate for status codes, but would still be fine here. Enums are intended for closed sets of values. By accepting an enum we say "here's the list of cases we handle. and nothing else". But status codes are extensible (both by RFC and in practice), e.g. 292 is a valid code that must be understood as a successful response. But the feature under discussion is a convenience one. By allowing Meanwhile the "appropriate" solution for status codes would be a class hierarchy where
@shaedrich what else would you need? The framework chooses between 200 and 201 automagically. You have |
There'd be 202 as well, for example, though it's way less frequently used than 200. |
I think I'm ok holding off on this since response with named arguments is not too bad: return response(status: 204); |
Well, the docs state:
So, giving the advise to use named arguments kind of goes against this rule |
Not contradictory in my opinion. Just a notice that in major versions of Laravel they can be renamed if the changed behavior of the logic requires it. |
Anytime I work on APIs I alway feel it would be smooth to quickly return a status code from a controller action.
However, this has underlying challenges - breaking change with existing apps and potentially unexpected side-effects of returning a raw integer.
So, in the spirit of the
to_route()
helper, I proposestatus
. Similarly this code reads very well, "return status 201". I've added a fluent API for named status.Examples
The fluent API method names are the snake case names of the Response constants.
Additional Examples