0

I'm trying to get an URL with route binding as one statement in routes/web.php matching the following pattern:

  • /abc/my-slug
  • /de/abc/my-slug
  • /es/abc/my-slug
  • etc.

Notably, the first parameter is optional should default to "en". Is it possible to have route binding to load a model from two parameters with one statement instead of two in the routes file?

1 Answer 1

0

Something like this maybe (untested) ?

 Route::name('page')
      ->get('/{lang?}/{slug}', [Controller::class, 'index'])
      ->setDefaults(['lang' => 'en']);

Or as an alternative read this on how to set defaults in a middleware

Sign up to request clarification or add additional context in comments.

3 Comments

I've tried this, it's leading either to "missing parameter" as the default doesn't seem to get set or to Required parameter $url_segment follows optional parameter $language if I set the optional parameter as default in the controller method.
And if you also set the slug as optional ?
The it would match other routes too. I've opted to have multiple route as I couldn't figure it without.

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.