Created
December 8, 2011 14:48
-
-
Save funkatron/1447169 to your computer and use it in GitHub Desktop.
Making a "catchall" route in Slim
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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' => '.+')); |
Amen!
This is what I needed.
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() {});
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
Why was this so hard to find? Also it works without the /api/ in case anyone is wondering.