ffflabs
ffflabs

Reputation: 17481

Retrieving query string parameters in Slim Framework 3

I have a get route in the form

$app->get('/redirect[/{subject}]', function ($request, $response, $args) {

});

If I make a request to /redirect/server?site=local&name=john

I can get the subject right with

$request->getAttribute('subject')

However, I can't get the query parameters. If I inspect $request->getQueryParams() I get:

[
  '/redirect/server?site' => 'local',
  'name' => 'john'
]

Whereas I'd be expecting the query params to be

[
  'site' => 'local',
  'name' => 'john'
]

What am I doing wrong? Perhaps I should specify that the url parameter mustn't accept a question mark?

Edit 1

I've been asked to post my webserver config. I'll have access to that dev machine tomorrow, so this is just a reminder to myself to add said info. However @jmattheis already gave me a hint.

Slim Framework 3 setup for nginx says something along the lines

location / {
        try_files $uri /index.php$is_args$args;
    }

Whereas I'm using the rewrite snippet that used to be suggested for slim 2 at some point in time:

location / {
        try_files $uri $uri/ /index.php?$request_uri;
    }

This config has worked fine for me for years, but it happens that I've never tried to parse whatever came after the question mark. It is just now that I have forked an abandoned project that I'm trying to transform raw $_REQUEST parsing into Slim routing methods.

Edit 2

The codebase for the project is in the repo phpPgAdmin6. It's basically a fork of phpPgAdmin that had no route logic, so I'm trying to centralize requests and responses to a given extent, and parse the query string using Slim native methods.

Upvotes: 5

Views: 5158

Answers (1)

jmattheis
jmattheis

Reputation: 11125

As you found out you need to use

location / {
    try_files $uri /index.php$is_args$args;
}

Instead of

location / {
    try_files $uri $uri/ /index.php?$request_uri;
}

The second one adds the whole request uri as a query parameter that would result in something like this

index.php?/redirect/server?site=local&name=john

because of that /redirect/server?site was given as query-key.

Upvotes: 3

Related Questions