powered by Slim Framework
Follow @NesbittBrian rss

Slim wildcard routes : Last but not least

Aug 3, 2012

This is a follow up to Slim wildcard routes improved... you may want to go and read that first.

With the release of Slim 1.6.5, route wildcard parameters are now officially part of the framework. Lets take at look at our past example and improve it again to use the baked in feature. As a reminder the example URIs we want to parse into the array are:


To indicate a route wildcard parameter we simply add a + after the name of the parameter, /api/getitems/:items+. We can now specify our route as follows:

$app->get('/api/getitems/:items+', function ($items) {

The output is printed as before for the three urls from above:

array(3) { [0]=> string(7) "seafood" [1]=> string(5) "fruit" [2]=> string(4) "meat" }
array(1) { [0]=> string(7) "seafood" }
array(5) { [0]=> string(7) "seafood" [1]=> string(5) "fruit" [2]=> string(6) "apples" [3]=> string(7) "bananas" [4]=> string(9) "chocolate" }

I'll finish off with something you might be wondering. You can specify additional parameters after the wildcard. The only gotcha here is that they can't be optional or the wildcard will gobble them all up and always leave the optional as the default value. I would describe this as expected behaviour.

$app->get('/api/getitems/:items+/:name', function ($items, $name) {

The above route will not match http://hostname/api/getitems/fruit but will match http://hostname/api/getitems/fruit/brian and http://hostname/api/getitems/seafood/fruit/meat/brian.

Slim wildcard routes improved  Home Introducing Carbon : A simple API extension for DateTime with PHP 5.3+  
blog comments powered by Disqus