Skip to content

Instantly share code, notes, and snippets.

@funkatron
Created December 8, 2011 14:48
Show Gist options
  • Save funkatron/1447169 to your computer and use it in GitHub Desktop.
Save funkatron/1447169 to your computer and use it in GitHub Desktop.
Making a "catchall" route in Slim
<?php
/**
* by setting the regex condition for the :method param to '.+', it matches
* everything, including "/" chars. this lets us match any URL of the format.
*
* /api/foo outputs "foo"
* /api/foo/bar/baz outputs "foo/bar/baz"
*/
$app->get('/api/:method', function($method) use ($app) {
echo $method;
})->conditions(array('method' => '.+'));
@montanaflynn
Copy link

Why was this so hard to find? Also it works without the /api/ in case anyone is wondering.

@Gazer
Copy link

Gazer commented Jun 5, 2013

Amen!

@alexweissman
Copy link

This is what I needed.

@stratoss
Copy link

stratoss commented Mar 13, 2017

This must be in the documentation! Thank you.

Based on that, we can allow all HEAD requests no matter of the actual route: $app->map(['HEAD'], '.+', function() {});

@michaelphipps
Copy link

In slim v4 I do this

$app->get('/{path:.*}', function ($request, $response, array $args) {
    // do stuff ...
});

If you have other routes, you need to make the catchall the last route so the other routes still function correctly. I found this method in the FastRoute library nikic/FastRoute#113

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment