Access single route parameter from Laravel route containing multiple parameters

66 Views Asked by At

I have a route defined as:

Route::domain('{subdomain}.' . config('app.domain'))->group(function () {
  Route::get('/some-url/{id}', function ($id) {
    return $id;
  });
});

When defining my routes, I utilize a domain parameter {subdomain} to capture the subdomain. However, upon accessing a URL like https://subdomain.example.com/some-url/1, I notice that the $id parameter in the closure contains the value of the subdomain (subdomain) instead of the intended ID (1). To address this issue, I must explicitly define both $subdomain and $id as parameters in the closure:

Route::domain('{subdomain}.' . config('app.domain'))->group(function () {
  Route::get('/some-url/{id}', function ($subdomain, $id) {
    return $id;
  });
});

By defining both parameters, I can accurately access the $id value. However, this approach becomes cumbersome when the subdomain parameter is unnecessary for all routes, complicating the closure signature unnecessarily.

Is there a more concise or efficient method to access route parameters without requiring them as dependencies in the closure functions?

0

There are 0 best solutions below