Slim PHP and GET Parameters

后端 未结 9 503
猫巷女王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:40

    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!

提交回复
热议问题