问题
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 => 9492
but 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