67

I'm using Slim PHP as a framework for a RESTful API.

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
starball
  • 20,030
  • 7
  • 43
  • 238
Eric Arenson
  • 797
  • 1
  • 6
  • 11

10 Answers10

118

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/

As of 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;
});
SirPilan
  • 4,649
  • 2
  • 13
  • 26
Martijn
  • 1,379
  • 1
  • 10
  • 13
32

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

Citing Slim 3 / Slim 4 documentation:

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

vlp
  • 7,811
  • 2
  • 23
  • 51
4

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.

Mulhoon
  • 1,852
  • 21
  • 26
3

Slim 3

$request->getQueryParam('page')

or

$app->request->getQueryParam('page')
Edmunds22
  • 715
  • 9
  • 10
2

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!

KlevisGjN
  • 681
  • 2
  • 10
  • 16
2

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

Cengkuru Michael
  • 4,590
  • 1
  • 33
  • 33
2

In Slim 3.0 the following also works:

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'));
        // ...
    }
}
Tamas Kalman
  • 1,925
  • 1
  • 19
  • 24
1

Inside your api controller function write the following code of line:

public function your_api_function_name(Request $request, Response $response)
    {
        $data = $request->getQueryParams();
        $zip = $data['zip'];
        $radius = $data['radius'];

    }

The variable $data contains all the query parameters.

Shinoy p b
  • 353
  • 3
  • 13
1

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.

George P
  • 736
  • 5
  • 12
  • 1
    George P and fire are right, I just had to use $_GET['var']. I thought maybe because of SlimPHP's built-in routing I'd have to do something fancy, but nope. Good old fashioned $_GET[] works just fine. Just overthinking it - thanks guys! – Eric Arenson Nov 14 '11 at 17:10
  • 1
    I would recommend sticking to $app->request->get('paramName'); even tho this does work. – Alex Mar 06 '17 at 13:05
  • @Alex can you explain why you would recommend that? – Holonaut Sep 18 '17 at 11:56
  • 1
    @Holonaut This is how the framework expects the parameters to be consumed. Consider in the future, a new middleware is added to escape, encode or trim white space. Your app using `$_SERVER['param']` would not get the benefit, where as, `$app->request()->get('paramName');` would – Alex Sep 19 '17 at 08:06
0

Probably obvious to most, but just in case, building on vip's answer concerning 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);
                }
            }
        }
bkudrle
  • 71
  • 7