HomeSlice
HomeSlice

Reputation: 612

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?

Upvotes: 2

Views: 1924

Answers (1)

Martin Bastien
Martin Bastien

Reputation: 186

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', '(.*)');

Upvotes: 11

Related Questions