Slim PHP and GET Parameters

2019-03-08 03:03发布

I'm playing with Slim PHP as a framework for a RESTful API, and so far it's great. Super easy to work with, but I do have one question I can't find the answer to. How do I grab GET params from the URL in Slim PHP?

For example, if I wanted to use the following:

http://api.example.com/dataset/schools?zip=99999&radius=5

A case of the Mondays? Am I overthinking it? Thanks in advance!

标签: php rest slim
7条回答
叼着烟拽天下
2楼-- · 2019-03-08 03:24

For Slim 3 you need to use the method getQueryParams() on the PSR 7 Request object.

Citing the documentation:

You can get the query parameters as an associative array on the Request object using getQueryParams().

You can also get a single query parameter value, with optional default value if the parameter is missing, using getQueryParam($key, $default = null).

查看更多
够拽才男人
3楼-- · 2019-03-08 03:25

You can do this very easily within the Slim framework, you can use:

$paramValue = $app->request()->params('paramName');

$app here is a Slim instance.

Or if you want to be more specific

//GET parameter

$paramValue = $app->request()->get('paramName');

//POST parameter

$paramValue = $app->request()->post('paramName');

You would use it like so in a specific route

$app->get('/route',  function () use ($app) {
          $paramValue = $app->request()->params('paramName');
});

You can read the documentation on the request object http://docs.slimframework.com/request/variables/

查看更多
狗以群分
4楼-- · 2019-03-08 03:32

IF YOU WANT TO GET PARAMS WITH PARAM NAME

$value = $app->request->params('key');

The params() method will first search PUT variables, then POST variables, then GET variables. If no variables are found, null is returned. If you only want to search for a specific type of variable, you can use these methods instead:

//--- GET variable

$paramValue = $app->request->get('paramName');

//--- POST variable

$paramValue = $app->request->post('paramName');

//--- PUT variable

$paramValue = $app->request->put('paramName');

IF YOU WANT TO GET ALL PARAMETERS FROM REQUEST WITHOUT SPECIFYING PARAM NAME, YOU CAN GET ALL OF THEM INTO ARRAY IN FORMAT KEY => VALUE

$data = json_decode( $app->request->getBody() ) ?: $app->request->params();

$data will be an array that contains all fields from request as below

$data = array(
    'key' => 'value',
    'key' => 'value',
    //...
);

Hope it helps you!

查看更多
我欲成王,谁敢阻挡
5楼-- · 2019-03-08 03:39

I fixed my api to receive a json body OR url parameter like this.

$data = json_decode($request->getBody()) ?: $request->params();

This might not suit everyone but it worked for me.

查看更多
劳资没心,怎么记你
6楼-- · 2019-03-08 03:45

Not sure much about Slim PHP, but if you want to access the parameters from a URL then you should use the:

$_SERVER['QUERY_STRING']

You'll find a bunch of blog posts on Google to solve this. You can also use the PHP function parse_url.

查看更多
神经病院院长
7楼-- · 2019-03-08 03:48

Use $id = $request->getAttribute('id'); //where id is the name of the param

查看更多
登录 后发表回答