Slim 3 - Placeholders in routes are not working

Hello!

Since my newest projekt with Slim3 I went into troubles with the routes. I try to explain by example testing routes:

$app->get(’/test/{hallo}’, function ($request, $response, $args) use ($app) {
print “test ok!”;
exit;
});

$app->post(’/test/{hallo}’, function ($request, $response, $args) use ($app) {
print “test ok!”;
exit;
});

$app->post(’/test’, function ($request, $response, $args) use ($app) {
print “test ok!”;
exit;
});

The routes
GET /test/1234
POST /test
are working as expected.

But for
POST /test/1234
I get: 405 Method not allowed. Must be one of: GET

I’m using latest Slim 3 and testing with Postman

What the hell am I doing wrong?
TIA for any help!
Regards from Austria

Try to move the $app->post(’/test/{hallo}’ route above the $app->get(’/test/{hallo}’ route.

same problem: 405 Method not allowed. Must be one of: GET

I have testet your routes with Slim 4 (not Slim 3) and everything works.
Maybe you try to upgrade to Slim 4?

slim 4 is no option because there are about 5 REST API’s which are running with slim 3 for this company, so they won’t change to a different system.

I haven’t had this problem with the existing 5 API’s. (Biggest one hast about 280 routes.)

The only difference here is: I’m using current Version of Slim and installed all with composer and my project with GIT. The other ones was more like copy & paste sources, and than rewrite the routes and functions.

For fun I also tried your routes in Slim 3 and it works too.

$app->get('/test/{hallo}', function ($request, $response, $args) {
    print "test ok! 1";
    exit;
});

$app->post('/test/{hallo}', function ($request, $response, $args) {
    print "test ok! 2";
    exit;
});

$app->post('/test', function ($request, $response, $args) {
    print "test ok! 3";
    exit;
});

image

seem’s like I’ll get one step back and just copy the sources from another working api, and replacing there routes.
something might be broken…

I’ve found the reason!
The regex for the placeholders doesn’t work like normal regex.
Following route caused the problems:
$app->post(’/buckets/{bucket_name:[a-Z0-9]+}’,

a-Z seems to break the method were this regex is used. Switched to
$app->post(’/buckets/{bucket_name:[a-zA-Z0-9]+}’,
and everything is fine now.

That cost me a lot of time :frowning:

Yes, the placeholder regex is case sensitive.