Skip to content

Instantly share code, notes, and snippets.

@funkatron
Created December 8, 2011 14:48
Show Gist options
  • Select an option

  • Save funkatron/1447169 to your computer and use it in GitHub Desktop.

Select an option

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
Copy Markdown

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

@Gazer

Gazer commented Jun 5, 2013

Copy link
Copy Markdown

Amen!

@alexweissman

Copy link
Copy Markdown

This is what I needed.

@stratoss

stratoss commented Mar 13, 2017 •

Copy link
Copy Markdown

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
Copy Markdown

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