Route conditions #43438
Unanswered
oprypkhantc
asked this question in
Ideas
Route conditions
#43438
Replies: 1 comment 2 replies
-
I'd like to have that logic in a more cleaner place. Like in a controller or an invokable class which you could pass by FQCN, just like you can do with the controller class. You'd still call that condition method inside your routes file, but the logic for that condition is placed elsewhere. This makes your routes file a lot cleaner and prevents spaghetti code... If laravel will not accept such a feature, something like this could easily be a package, as Route is macroable. |
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hey.
Symfony has a feature in their router called conditions. It basically allows specifying additional conditions for a route to match through their proprietary language. This comes in handy when dealing with dynamic routes - e.g. those that depend on the host, for example. Laravel's routing is limited to regular expressions, but sometimes you need more than that, like find a user in the DB from a subdomain and route based on that.
I'm proposing adding a
->condition(callable $condition)
method onRoute
. Callable acceptsRequest
and returnstrue
if route matches orfalse
if router should find a different route (or a fallback route, or display a 404).This plays nicely with both uncached and cached routes. The former just need a new
Validator
and the latter can provide acondition
callback to Symfony's URI matcher by setting index4
here:https://github.com/laravel/framework/blob/9.x/src/Illuminate/Routing/RouteCollection.php#L262
https://github.com/symfony/routing/blob/6.1/Matcher/Dumper/CompiledUrlMatcherDumper.php#L126
I'd love to implement this, but I don't want to waste time if it's declined, hence asking here.
@driesvints Any chance for this feature to be accepted?
Beta Was this translation helpful? Give feedback.
All reactions