Slim 3 - Route Regex/Dynamic Route


Hello folks,

I have two principal questions, one is kinda linked to another.

Question One: Regex Routing

I wanted to do “dynamic” routing, so I used RegEx to match something like this:

GET - api/library/2/books/3/5/10/33/…/pages

I declared the route like this ( Don’t pay too much attention on the code its just an example ):

$app->group('/library', function () {

     $this->group('/{library_id}', function(){

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

                 $this->get('',BooksController::class . ':getSelectedBooks');


The thing is that when I use $request->getAttribute(‘routeInfo’)[2] I get something like that:

“books_id”: “1/2/3/4”

I know I can always explode that and turn it into an array but does Slim have this functionality embedded ?

Question Two: Recomended Method to do that.

The operation above retrieve of all books only the ones that are into the request. It is clearly a GET operation.

From your perspective what is better ( pro/cons ) of doing those two bellow:

  • Using GET

GET - api/library/2/books/3/5/10/33/…/pages

  • Using POST

POST - api/library/2/books/pages


	"books_id": [



If I’m understanding your question correctly, Slim provides a solution to this in their routing schema:

$app->get('/library[/{books:.*}]',function($request,$response,$params) {
  $books = explode('/',$params['books']);

The books route array can be accessed by index order:

$books[0], $books[1], etc.

You still need to explode the route but it’s cleaner than nested route groups imo.


Actually inside the groups there are more EndPoints thought. Thats the way I doing it in the moment.

I’m in a real doubt here between using POST or GET methods to implement that. Books Ids on URL will get really messy if there was like 100 books to retrieve…


I finally come to a conclusion to this. I will not use slashes ( / ) to separate my multiple resources because slashes remind hierarchy and thats not what I want.

I decided to do something like this:

GET - api/library/2/books/2,3,5,6,…,n/pages

Thanks for the help @robrothedev!