#Same uri path with different 'where' for matching different controllers

1 messages · Page 1 of 1 (latest)

woeful sluice
#

I have a use-case where I have the same uri path, e.g.: /{locale}/{customer}/{form}, once attached with ->where('customer', 'foo') and once with ->where('customer', 'bar'). The reason is, they need to use two different controllers for handling the forms. When I defined that, only one form ever works, depending on the order of the route.

How would you approach this use-case where you have the same uri path but two different controllers, depending on the route parameter?

sacred creek
#
Route::post('{locale}/foo/{form}', [FooFormController::class, 'store'])
Route::post('{locale}/bar/{form}', [BarFormController::class, 'store'])
#

Why cant you just do that?

woeful sluice
#

There can be multiple foo or bars.

#

one group is e.g.: foo, foobar, barfoo and the other group is bar, baroof, etc. Both can be multiple values.

#

like, customers x, y and z need controller Foo, customers a, b and c need controller Bar.

woeful sluice
#

that's because how the forms are handled. We have two approaches which are not compatible with each other.

sacred creek
#

But what are these forms? Why is the endpoint the same for two entirely separate tools?

woeful sluice
#

they are just very similar, one route has more customization attached to it

#

but I might add some kind of filler to the uri path if that is a problem.

#

I thought my approach would work since the "where" limits which route are taken into consideration.

sacred creek
#

It seems like this is supposed to be some kind of "one controller to rule them all" situation since you are passing what form is being submitted as a url parameter. Really each form should have a unique controller method. If you see yourself reusing code in a lot of controller methods then extract the reusable code into its own function/trait instead of merging all the controller methods into one

woeful sluice
#

the approach is a little bit complexer and doing it this way actually keeps the code DRY. But I'll rethink how I could solve it.