{"id":13805868,"url":"https://github.com/entomb/slim-json-api","last_synced_at":"2025-04-13T05:07:14.084Z","repository":{"id":8514866,"uuid":"10127859","full_name":"entomb/slim-json-api","owner":"entomb","description":"Slim extension to implement fast JSON API's","archived":false,"fork":false,"pushed_at":"2017-04-30T12:15:11.000Z","size":75,"stargazers_count":268,"open_issues_count":11,"forks_count":49,"subscribers_count":24,"default_branch":"master","last_synced_at":"2025-04-13T05:07:05.488Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/entomb.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2013-05-17T16:48:46.000Z","updated_at":"2024-05-12T21:44:32.000Z","dependencies_parsed_at":"2022-09-17T07:51:50.467Z","dependency_job_id":null,"html_url":"https://github.com/entomb/slim-json-api","commit_stats":null,"previous_names":[],"tags_count":12,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/entomb%2Fslim-json-api","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/entomb%2Fslim-json-api/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/entomb%2Fslim-json-api/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/entomb%2Fslim-json-api/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/entomb","download_url":"https://codeload.github.com/entomb/slim-json-api/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248665747,"owners_count":21142123,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-04T01:01:05.770Z","updated_at":"2025-04-13T05:07:14.064Z","avatar_url":"https://github.com/entomb.png","language":"PHP","funding_links":[],"categories":["Miscellaneous","Packages and Middleware"],"sub_categories":["Videos"],"readme":"# slim-jsonAPI\n[![Latest Stable Version](https://poser.pugx.org/entomb/slim-json-api/v/stable.png)](https://packagist.org/packages/entomb/slim-json-api)\n[![Total Downloads](https://poser.pugx.org/entomb/slim-json-api/downloads.png)](https://packagist.org/packages/entomb/slim-json-api)\n\nThis is an extension to the [SLIM framework](https://github.com/codeguy/Slim) to implement json API's with great ease.\n\n## Installation\nUsing composer you can add use this as your composer.json\n\n```json\n    {\n        \"require\": {\n            \"slim/slim\": \"2.3.*\",\n            \"entomb/slim-json-api\": \"dev-master\"\n        }\n    }\n\n```\n\n## Usage\nTo include the middleware and view you just have to load them using the default _Slim_ way.\nRead more about Slim Here (https://github.com/codeguy/Slim#getting-started)\n\n```php\n    require 'vendor/autoload.php';\n\n    $app = new \\Slim\\Slim();\n\n    $app-\u003eview(new \\JsonApiView());\n    $app-\u003eadd(new \\JsonApiMiddleware());\n```\n\n### .htaccess sample\nHere's an .htaccess sample for simple RESTful API's\n```\nRewriteEngine On\nRewriteCond %{REQUEST_FILENAME} !-f\nRewriteRule ^ index.php [QSA,L]\n```\n\n### example method\nall your requests will be returning a JSON output.\nthe usage will be `$app-\u003erender( (int)$HTTP_CODE, (array)$DATA);`\n\n#### example Code \n```php\n\n    $app-\u003eget('/', function() use ($app) {\n        $app-\u003erender(200,array(\n                'msg' =\u003e 'welcome to my API!',\n            ));\n    });\n\n```\n\n\n#### example output\n```json\n{\n    \"msg\":\"welcome to my API!\",\n    \"error\":false,\n    \"status\":200\n}\n\n```\n\n## Errors\nTo display an error just set the `error =\u003e true` in your data array.\nAll requests will have an `error` param that defaults to false.\n\n```php\n\n    $app-\u003eget('/user/:id', function($id) use ($app) {\n\n        //your code here\n\n        $app-\u003erender(404,array(\n                'error' =\u003e TRUE,\n                'msg'   =\u003e 'user not found',\n            ));\n    });\n\n```\n```json\n{\n    \"msg\":\"user not found\",\n    \"error\":true,\n    \"status\":404\n}\n\n```\n\nYou can optionally throw exceptions, the middleware will catch all exceptions and display error messages.\n\n```php\n\n    $app-\u003eget('/user/:id', function($id) use ($app) {\n\n        //your code here\n\n        if(...){\n            throw new Exception(\"Something wrong with your request!\");\n        }\n    });\n\n```\n```json\n{\n    \"error\": true,\n    \"msg\": \"ERROR: Something wrong with your request!\",\n    \"status\": 500\n}\n\n```\n\n## Embedding response data and metadata in separate containers\nIt is possible to separate response metadata and business information in separate containers.\n\n#### To make it possible just init JsonApiView with containers names\n```php\n   require 'vendor/autoload.php';\n\n    $app = new \\Slim\\Slim();\n\n    $app-\u003eview(new \\JsonApiView(\"data\", \"meta\"));\n    $app-\u003eadd(new \\JsonApiMiddleware());\n```\n\n#### Response\n```json\n{\n    \"data\":{\n        \"msg\":\"welcome to my API!\"\n    },\n    \"meta\":{\n        \"error\":false,\n        \"status\":200\n    }\n}\n```\n\n\n## routing specific requests to the API\nIf your site is using regular HTML responses and you just want to expose an API point on a specific route,\nyou can use Slim router middlewares to define this.\n\n```php\n    function APIrequest(){\n        $app = \\Slim\\Slim::getInstance();\n        $app-\u003eview(new \\JsonApiView());\n        $app-\u003eadd(new \\JsonApiMiddleware());\n    }\n\n\n    $app-\u003eget('/home',function() use($app){\n        //regular html response\n        $app-\u003erender(\"template.tpl\");\n    });\n\n    $app-\u003eget('/api','APIrequest',function() use($app){\n        //this request will have full json responses\n\n        $app-\u003erender(200,array(\n                'msg' =\u003e 'welcome to my API!',\n            ));\n    });\n```\n\n\n## middleware\nThe middleware will set some static routes for default requests.\n**if you dont want to use it**, you can copy its content code into your bootstrap file.\n\n***IMPORTANT: remember to use `$app-\u003econfig('debug', false);` or errors will still be printed in HTML***\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fentomb%2Fslim-json-api","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fentomb%2Fslim-json-api","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fentomb%2Fslim-json-api/lists"}