Slim 3 - Regex *RegExpression* for parameters *placeHolder* contains a capturing group

Hello again folks,

As the title says I having a problem with my routing, especially this one:

Note: Don’t mind the group here with a single EndPoint, there is a lot of EndPoints inside but showing them its not the foccus here.

 $this->group('/books/{books_id:(\d+,{0,1})*\d+}', function(){

            $this->get('',ResourceController::class . ':getSelectedResources');

        });

and I got this error:

"message": "Regex \"(\\d+,{0,1})*\\d+\" for parameter \"books_id\" contains a capturing group"

My expected EndPoint is something like this:

GET - /api/books/1,2,3,4,5,…,n/pages

I have tested my Regular Expression at: http://regexr.com/

and used this exact expression: (\d+,{0,1})*\d+

Thanks,
LosLobos.

Hi,

try with this:

$this->group('/books/{books_id:[0-9]+}', function(){
            $this->get('',ResourceController::class . ':getSelectedResources');
});

This will generate the following route pattern:

/api/books/21323131…

and not

/api/books/2,3,4,5,1,2,…,n

Thanks,
LosLobos.

Apparantly capturing groups are not allowed, which is what you get by using parantheses. Instead you can use a non-capturing groups:

// note the use of (?: instead of ( in the regular expression
$this->group('/books/{books_id:(?:\d+,{0,1})*\d+}', function(){
   $this->get('', ResourceController::class . ':getSelectedResources');
});

Hope that helps,

Lennaert

1 Like

It works ! But its very strange thought. I’ll read further about this.

Thanks for your help ! Really appreciated!

The reason for this is that FastRoute internally compiles a regular expression to match all routes in one go. Capturing groups would break this.