Getting request parameters on Slim

心已入冬 提交于 2020-01-13 11:00:12

问题


I'm trying to get request parameter names and values dynamically, but the array is always empty. This is the get route:

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

And this is how im calling it from the browser:

http://localhost/get/profile/9492

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

Any ideas why?


回答1:


Notice: Please read update notes before trying out this code. The update note is my first comment in this answer.

Couldn't get to test it but please 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: Okay after some digging figured the following, the params() method requires a parameter. If called without a parameter a Notice is raised. Checking the source revealed 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 either to retrieve parameters so you have to use the function parameter $id_user which has the expected value.




回答2:


You can use following:

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



回答3:


Also is a configuration problem。

try

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


来源:https://stackoverflow.com/questions/17056281/getting-request-parameters-on-slim

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!