Getting request parameters for Slim

I am trying to get the names and values โ€‹โ€‹of query parameters dynamically, but the array is always empty. This is the route to receive:

$app->get('/get/profile/:id_user', function ($id_user) use ($app) { print_r($app->request()->params()); }); 

And this is how it calls from the browser:

 http://localhost/get/profile/9492 

This should return an array with id_user => 9492 , but it is empty.

Any ideas why?

+4
source share
3 answers

Note: Read the upgrade notes before trying this code. The update note is my first comment in this answer.

Verification failed, but try the following:

 $app->get('/get/profile/:id_user', function ($id_user) use ($app) { $req = $app->request(); print_r($req->params()); }); 

Reference documentation: http://docs.slimframework.com/#Request-Method

Update: Well, after some digging it turned out the following, the params() method requires a parameter. If called without a parameter, a notification occurs. Checking the source showed that this function, called without a parameter, returns null. See Http/Request.php line 199. Also, for some reason, currying does not seem to work for parameter extraction, so you need to use the function parameter $id_user , which has the expected value.

+4
source

You can use the following:

 $app->get("/test.:format/:name",function() use ($app){ $router = $app->router(); print_r($router->getCurrentRoute()->getParams()); }); 
+3
source

There is also a configuration problem.

try

 try_files $uri $uri/ /index.php?$query_string; 
0
source

Source: https://habr.com/ru/post/1485717/


All Articles