I often use multi-domain applications (e.g. multi-tenant) that require the domain to be part of a URL when using route(). Thus, the domain is needed as a route parameter, which is defined like this in routes/web.php:
Route::group([
'domain' => '{domain}',
], function () {
Route::get('{someModel}', [AppHttpControllersSomeModelController::class, 'show']);
// ... some other routes
});
RouteServiceProvider.php:
Route::model('someModel', SomeModel::class);
SomeModelController.php:
// this works but needs the $domain variable in the signature that is not needed in the code below
public function show(string $domain, SomeModel $someModel): JsonResponse
{
return response()->json($context);
}
// this is what I want to be using, but causes an error: "Argument #1 ($someModel) must be of type AppModelsSomeModel, string given"
public function show(SomeModel $someModel): JsonResponse
{
return response()->json($context);
}
As shown, some controller methods do not need the $domain variable (while others do). However, the variable needs to be injected as the controller expects the variables in the same order as in the route definition chain. I do understand where the error is coming from but I think I’m missing something in setting up dependency injection (DI) as there is no need for $domain. DI should be able to see by the variable name “someModel” that domain is not needed here.
I do not want to remove domain from the route parameters for several reasons. I just want to omit it in the controller method signatures if not needed.
Does anyone know how?