Slim JSON Outputs

前端 未结 17 1670
一整个雨季
一整个雨季 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:56

    I use https://github.com/entomb/slim-json-api for my API written in Slim 2 to enable JSON-response. Init code looks something like this:

    function APIRequests () {
        $app = \Slim\Slim::getInstance();
        $app->view(new \JsonApiView());
        $app->add(new \JsonApiMiddleware());
    }
    
    $app->group('/api', 'APIRequests', function () use ($app) {
        $app->get('/areas/:id', function ($id) use ($app) {
           $app->render(200, Area::find($id));
        });
    });
    

    I really like the abstraction level using middleware and grouping of routes, making it easy to apply different response types to different areas of the app.

提交回复
热议问题