Support the ongoing development of Laravel.io →
Installation Configuration Requests

So I'm trying to match a simple route with laravel that looks something like

/ID-OPTIONAL/OPTIONAL2/

So in order to get the hyphen to be optional I needed to use laravels where function and did something like so:

Route::any('/{id}{param?}/{param2?}', 'SomeController@Index', function($id = 0, $param = null, $param2 = null){})
->where(array('id' => '\d+', 'param' => '[A-Za-z-_]+'));

Now visiting

 /10-some-random-param/another-param
 /10-some-random-param/

Works fine, but when I all of a sudden do

 /10/another-param

I get a routing error. Now is it possible to route this in one route or am I forced to write another line in order to get the expected result. Or is this a bug with laravels routing?

Last updated 2 years ago.
0

Eh, so basically I went w/ the following

 Route::any('/{id}/{optional?}', 'HomeController@Index', function($id, $optional) {})
 ->where(array('id' => '(\d+)-?([A-Za-z0-9-_]+)?'));

Which isn't what I really wanted since I have to split the id w/ another regex check in the actual controller

Last updated 2 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

G4MR g4mr Joined 10 Feb 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.