Search code examples
routeslaravelwildcardlaravel-3

Laravel 3.x Routes issue - Getting wrong param value using wildcard


I'm trying to use some wildcards in my Routes file because it was getting too bloated. Let's say I have a photos and a galleries route and I have decided to locate them under a "media" "subroute":

Route::get('admin/media/(:any)/edit/(:num)', function($p) {
    dd($p);
});

The die and dump gives me "galleries" by acessing "http://www.bossplaya.dev/admin/media/galleries/edit/1" instead of "1", as expected.

But THIS works:

Route::get('admin/media/galleries/edit/(:num)', function($p) {
    dd($p); // Returns "1"
});

Is there a way to use wildcards like this? That would save me a lot of time and make my routes file much cleaner.

Any ideas?


Solution

  • Each wildcard you add to the URL will become a parameter in your route, so in this example you'll find $type becomes 'galleries' and $id becomes '1'...

    Route::get('admin/media/(:any)/edit/(:num)', function($type, $id) {
        dd(compact('type', 'id'));
    });
    

    If there is any reason you do not want to know what the (:any) is then you could use a non-matching regular expression, for example:

    Route::get('admin/media/[^/]+/edit/(:num)', function($id) {
        dd(compact('id'));
    });