The Slim Framework support forum has moved to This Tender forum is no longer maintained or monitored.

What's the best way to make an empty route parameter throw a 400 (instead of returning a Slim 404 page)?

Jonathan Harford's Avatar

Jonathan Harford

23 Jun, 2015 06:15 PM

I have a route like so:

$app->get('/search/:term/suggestions', function ($term) use ($app) { ... });
This gives the Slim 404 page when :term is a blank string, and I'd to instead return a 400.

Is the best thing to do this?

$app->get('/search/(:term)/suggestions', function ($term = null) use ($app) {
    if (is_null($term)) throw new BadRequestException('Empty search parameter.');
This works, but feels hacky enough that I wanted to ask (since I'm saying :term is optional when it's not).

Or should I just not allow for double-slashes in my clients? Some say that such URIs violate the specification, but this page says it's A-OK.

Reply to this discussion

Internal reply

Formatting help / Preview (switch to plain text) No formatting (switch to Markdown)

Attaching KB article:


Attached Files

You can attach files up to 10MB

If you don't have an account yet, we need to confirm you're human and not a machine trying to post spam.

Keyboard shortcuts


? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac