Andres SK
Andres SK

Reputation: 10974

Getting request parameters on Slim

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?

Upvotes: 3

Views: 16379

Answers (3)

fainle
fainle

Reputation: 11

Also is a configuration problem。

try

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

Upvotes: 0

FLEXROAD
FLEXROAD

Reputation: 43

You can use following:

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

Upvotes: 3

vee
vee

Reputation: 38645

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.

Upvotes: 4

Related Questions