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' => '.+'));
@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