spekulatius
spekulatius

Reputation: 1499

Laravel URL with optional parameter at the beginning

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

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?

Upvotes: 0

Views: 283

Answers (1)

CoolGoose
CoolGoose

Reputation: 508

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

Upvotes: 0

Related Questions