Replies: 1 comment
-
As explicit route binding is done in the boot part of your service provider, the request object is already created and also the UrlGenerator. So I you could do something like this: $request = $this->app->make(\Illuminate\Http\Request::class);
if ($request->is('api/seller/*')) {
Route::bind('test', function ($id) {
return Product::findOrFail($id);
});
} else {
Route::bind('test', function ($id) {
return Product::published()->findOrFail($id);
});
} Or have the if inside the binding closure, or have separated methods like this: $request = $this->app->make(\Illuminate\Http\Request::class);
if ($request->is('api/seller/*')) {
$this->addBindingsForSeller();
} else {
$this->addBindingsForCustomer();
} And set the bindings inside these custom methods. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I have the following scenario:
Seller module:
Route 1:
/api/seller/{seller}/product/{product}
Route-model binding:
Product module:
Route 2:
/api/product/{product}
In the
Seller
module, sellers can view their products, edit them, delete them etc.In the
Product
module, users can list only published products.This clearly won't work - the second route will override the first route, because of this:
Where the last code snippet is from
Illuminate\Routing\Router
.Wouldn't it be better if we have contectual loading, based on the uri. Something like this:
So, everythime a route starts with
/api/seller/{seller}/product/{product}/...
, the first case will be loaded. And if the route starts with/api/product/{product}/...
, the second case will be loaded.Of course if you think this makes sense, I will take the initiative to implement it.
Beta Was this translation helpful? Give feedback.
All reactions