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


#1

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.


#2

Hi,

try with this:

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

#3

This will generate the following route pattern:

/api/books/21323131…

and not

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

Thanks,
LosLobos.


#4

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


#5

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

Thanks for your help ! Really appreciated!


#6

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