Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 4 Routing with Arbitrary Number of URL Segments

I have an L3 application I'm trying to port to L4. In the L3 version, one of my routes is

Route::get('/(:any)/(:all?)', etc...

This allowed me to handle an arbitrary number of URL segments, for example:

/contact_page
/store_category
/store_category/shirts_category
/store_category/shirts_category/specific_shirt_page
/an/arbitrary/number/of/nested/categories

But in L4 I cannot figure out how to emulate the functionality of (:all?)

The code below works:

Route::get('/{arg1?}/{arg2?}/{arg3?}', function($arg1='home', $arg2, $arg3)
{
  //do something
});

So I could add a large number of optional arguments (more than I think I would ever need in real world use) but that is not very elegant.

Is there some way in Laravel 4 to define a Route that can respond to an arbitrary number of URL segments?

like image 308
HomeSlice Avatar asked Jan 28 '26 04:01

HomeSlice


1 Answers

You can add a pattern condition to your route, ex.:

Route::get('{any}/{args}', function($action, $args = null)
{
   // do something like return print_r(explode('/', $args), true);
})->where('args', '(.*)');
like image 128
Martin Bastien Avatar answered Jan 29 '26 20:01

Martin Bastien



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!