Thin PHP and GET parameters

I play with Slim PHP as a framework for a RESTful API, and so far it's great. Super easy to work, but I have one question that I can not find the answer to. How do I get GET parameters from a URL in Slim PHP?

For example, if I wanted to use the following:

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

The case from Monday? Am I overdoing it? Thanks in advance!

+54
rest php slim
Nov 14 2018-11-11T00:
source share
8 answers

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

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

$ app is a subtle example.

Or if you want to be more specific

// GET parameter

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

// POST parameter

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

Would you use it like that in a particular route

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

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

Starting with Slim v3 :

 $app->get('/route', function ($request, $response, $args) { $paramValue = $request->params(''); // equal to $_REQUEST $paramValue = $request->post(''); // equal to $_POST $paramValue = $request->get(''); // equal to $_GET // ... return $response; }); 
+103
Dec 31 2018-11-11T00:
source share

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

Documentation citation:

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

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

+22
08 Oct '16 at 18:57
source share

I fixed my api to get json body OR url parameter similar to this.

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

It may not suit everyone, but it worked for me.

+3
Apr 09 '13 at 11:01
source share

Slim 3.0 also works as follows:

routes.php

 require_once 'user.php'; $app->get('/user/create', '\UserController:create'); 

user.php

 class UserController { public function create($request, $response, array $args) { $username = $request->getParam('username')); $password = $request->getParam('password')); // ... } } 
+2
Jun 19 '17 at 0:30
source share

I don't know much about Slim PHP, but if you want to access parameters from a URL, you should use:

 $_SERVER['QUERY_STRING'] 

You will find tons of blog posts on Google to solve this problem. You can also use the PHP function parse_url .

+1
Nov 14 '11 at 17:05
source share

IF YOU WANT TO GET PARAMS WITH THE NAME PARAM

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

The params () method will first look for PUT variables, then POST variables, and 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 THE PARAMETERS FROM THE REQUEST, NOT SPECIFYING THE PARAMETER NAME, YOU CAN GET ALL OF THEM IN ARRAY IN THE FORMAT KEY => PRICE

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

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

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

Hope this helps you!

+1
Jan 26 '16 at 18:52
source share

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

+1
Apr 12 '17 at 15:32
source share

This is probably obvious to most, but just in case, based on the vip answer regarding Slim 3 , you can use something like the following to get the values ​​for the parameters.

  $logger = $this->getService('logger'); $params = $request->getQueryParams(); if ($params) { foreach ($params as $key => $param) { if (is_array($param)) { foreach ($param as $value) { $logger->info("param[" . $key . "] = " . $value); } } else { $logger->info("param[" . $key . "] = " . $param); } } } 
0
May 04 '19 at 16:28
source share



All Articles