Slim JSON Outputs

前端 未结 17 1706
一整个雨季
一整个雨季 2021-01-30 09:00

I am using the Slim framework with PHP to create a RESTful API for my app. However, I assumed that the framework would have some way of creating easier JSON outputs rather than

17条回答
  •  庸人自扰
    2021-01-30 09:42

    Using Slim 3, I'm using this format:

    get('/{id}', function ($request, $response, $args) {
        $id = $request->getAttribute('id');
    
        return $response->withJSON(
            ['id' => $id],
            200,
            JSON_UNESCAPED_UNICODE
        );
    });
    

    On request "/123", result JSON with:

    {
      id: "123"
    }
    

    More infos read here.

    [UPDATE] Added second and third param to withJSON. Second is The HTTP status code, and third is Json encoding options (best for especial chars and others, for example: print "ã" correctly)

提交回复
热议问题