-
Notifications
You must be signed in to change notification settings - Fork 11k
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
[11.x] Allow string backed enums for route name()
and domain()
#51931
[11.x] Allow string backed enums for route name()
and domain()
#51931
Conversation
What about non backed enums? |
Explicitly checking for backed enums. |
That is not what I was hoping for. I am not that keen on typing things twice if I don't need to. I'll keep writing '->name' then. |
Following the same principle as it was decided for implicit route enum binding: https://laravel.com/docs/11.x/routing#implicit-enum-binding Personally, I think magically converting all kind of cased names for this purpose isn't the way. Let's wait and see what Taylor thinks. 🙏 |
Personally I don't think we should magically convert anything. If you want lower case for some reason you can use the string backed enums, otherwise it is fine the way your enum case is defined. |
I think it would be perfectly fine if a non-backed enum like That would avoid manual work when defining the route enums, and that would also help with keeping a convention. As @henzeb said, if you want a specific case or formatting, you could use string-backed enums. |
Instead of all of this refactoring, could you just have a class with a bunch of constants? <?php
namespace App;
class AuthRoutes
{
public const string LoginView = 'something';
public const string RegisterView = 'something-else';
} |
@taylorotwell nah. The main benefit of enums is self-validation - duplicate values are impossible. Constants, unlike enums, don't have it. 🤷♂️ |
Nah, with string backed enums anyone can change the value without breaking the code. Just one character uppercase is enough to be a difference. And even constants show if they are used or not. So that is not a benefit either. |
#52561 ✅ |
We recently found ourselves adopting string backed enums to manage route names. Our app only has a few hundred named routes. But combined with
n
references to them, that is a lot of string-y stuff. If things get messy in our medium sized app, I can see this approach being helpful for others.One small annoyance, however, is that we now have to call
->value
on the enums a ton of times. Our code would look cleaner if we could pass the enum directly to methods likeRoute::name()
andRedirect::route()
.This PR seeks to allow this.
Proposal
Before:
After:
Why Enums
$menu->route()
,Form::model($model, ['method' => 'PUT', 'files' => true, 'route' => []])
,route()
,Redirect::route()
,modal(route())
,Route::name()
, and most likely still not catach all. In addition, you deal with potentially conflicting strings fromconfig()
,view()
, named services, middleware aliases, custom methods, facades, channels etc.). Or you go through them one by one. It's painful.I think it is obvious that using enums instead of strings adds a lot of value.
Notes
Route::has()
also supports the feature, and that I did not miss anything.