Slim PHP and GET Parameters

后端 未结 9 511
猫巷女王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: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;
    });
    

提交回复
热议问题