0

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?

8
  • 3
    $request->route()->parameter(...) Commented Feb 22, 2024 at 4:49
  • 1
    Can you fix the syntax errors in your code so it's clear what you're trying to do? Commented Feb 22, 2024 at 21:00
  • @miken32, I have fixed the syntax error. Commented Feb 24, 2024 at 0:53
  • So you're putting these routes in a group with a parameter, but you don't want to use the parameter? Why not just leave out the parameter then? Commented Feb 24, 2024 at 20:23
  • @miken32, subdomain parameter is required for grouping so that I can access these routes under dynamic subdomains to set currentTenant as it is multi-tenant application. I just don't need subdomain parameter in Controllers, hence I do not pass these as dependency to controllers. But I need to pass both subdomain and id in controller dependency even if I want only id in controller logic. Commented Feb 25, 2024 at 11:35

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.