Slim PHP and GET Parameters

后端 未结 9 498
猫巷女王i
猫巷女王i 2021-01-30 03:56

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 gr

相关标签:
9条回答
  • 2021-01-30 04:26

    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);
                    }
                }
            }
    
    0 讨论(0)
  • 2021-01-30 04:28

    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;
    });
    
    0 讨论(0)
  • 2021-01-30 04:30

    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.

    0 讨论(0)
  • 2021-01-30 04:35

    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().

    0 讨论(0)
  • 2021-01-30 04:38

    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.

    0 讨论(0)
  • 2021-01-30 04:39

    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'));
            // ...
        }
    }
    
    0 讨论(0)
提交回复
热议问题