{"_id":"57ffd85f755a2e1700c2bb26","user":"567082433a32d20d00c45cab","category":{"_id":"57ffd85f755a2e1700c2bb0a","version":"57ffd85f755a2e1700c2bb05","__v":0,"project":"567083021dabd80d00b122ae","sync":{"url":"","isSync":false},"reference":false,"createdAt":"2015-12-18T23:14:14.145Z","from_sync":false,"order":4,"slug":"rest-api","title":"REST API"},"parentDoc":null,"version":{"_id":"57ffd85f755a2e1700c2bb05","project":"567083021dabd80d00b122ae","__v":1,"createdAt":"2016-10-13T18:54:23.018Z","releaseDate":"2016-10-13T18:54:23.018Z","categories":["57ffd85f755a2e1700c2bb06","57ffd85f755a2e1700c2bb07","57ffd85f755a2e1700c2bb08","57ffd85f755a2e1700c2bb09","57ffd85f755a2e1700c2bb0a","57ffd85f755a2e1700c2bb0b","57ffd85f755a2e1700c2bb0c","57ffd85f755a2e1700c2bb0d"],"is_deprecated":false,"is_hidden":false,"is_beta":false,"is_stable":true,"codename":"","version_clean":"3.4.0","version":"3.4"},"__v":0,"project":"567083021dabd80d00b122ae","updates":[],"next":{"pages":[],"description":""},"createdAt":"2015-12-18T23:17:55.362Z","link_external":false,"link_url":"","githubsync":"","sync_unique":"","hidden":false,"api":{"results":{"codes":[]},"settings":"","auth":"required","params":[],"url":""},"isReference":false,"order":1,"body":"[Response macros](http://laravel.com/docs/5.1/responses#response-macros) are originally a feature in the Laravel framework.\n\nSince we mentioned that REST APIs need to be consistent, we've provided you with 2 default response macros that will let you return success and error data from your endpoints.\n\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"<?php\\n\\nclass PostsController\\n{\\n    public function get()\\n    {\\n      \\t$posts = App\\\\Post::all();  \\n      \\n    \\t\\treturn response()->success('posts', $posts);\\n    }\\n  \\n}\",\n      \"language\": \"php\"\n    }\n  ]\n}\n[/block]\n\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"<?php\\n\\nclass SettingsController\\n{\\n    public function update()\\n    {\\n\\t\\t\\t\\tif ( !\\\\Auth::user()->is_verified ){\\n          return response()->error('Not Authorized', 401);\\n        }\\n    }\\n  \\n}\",\n      \"language\": \"php\"\n    }\n  ]\n}\n[/block]\nNote that even validation errors through `$this->validate($request, [])` will return the same error format as `response()->error()` but with a status code of 422 (Unprocessable Entity).\n\nThis means that you can always expect the same response format for all success responses.\nAnd another unified response format for all error responses (including validations).\n\nThis repository takes advantage of consistency, by configuring [Restangular](doc:restangular) to show validation errors in a **Toast message**.\n\nOf course, there are [API test helpers](doc:api-test-helpers) that are provided so you can easily assert the correct response macro is being used (also makes sure your return types are consistent).\nFor example `->seeApiSuccess()`, `->seeValidationError()`, `->seeApiError(401)`.\n[block:callout]\n{\n  \"type\": \"warning\",\n  \"title\": \"Validation response\",\n  \"body\": \"If you plan on modifying the *error* response macro, you'd need to apply a fix in `App\\\\Http\\\\Controllers\\\\controller.php` which is the base controller.\"\n}\n[/block]\n> Prefer a blogpost?\n> [https://blog.jadjoubran.io/2016/03/27/laravel-response-macros-api/](https://blog.jadjoubran.io/2016/03/27/laravel-response-macros-api/)","excerpt":"","slug":"response-macros","type":"basic","title":"Response Macros"}
[Response macros](http://laravel.com/docs/5.1/responses#response-macros) are originally a feature in the Laravel framework. Since we mentioned that REST APIs need to be consistent, we've provided you with 2 default response macros that will let you return success and error data from your endpoints. [block:code] { "codes": [ { "code": "<?php\n\nclass PostsController\n{\n public function get()\n {\n \t$posts = App\\Post::all(); \n \n \t\treturn response()->success('posts', $posts);\n }\n \n}", "language": "php" } ] } [/block] [block:code] { "codes": [ { "code": "<?php\n\nclass SettingsController\n{\n public function update()\n {\n\t\t\t\tif ( !\\Auth::user()->is_verified ){\n return response()->error('Not Authorized', 401);\n }\n }\n \n}", "language": "php" } ] } [/block] Note that even validation errors through `$this->validate($request, [])` will return the same error format as `response()->error()` but with a status code of 422 (Unprocessable Entity). This means that you can always expect the same response format for all success responses. And another unified response format for all error responses (including validations). This repository takes advantage of consistency, by configuring [Restangular](doc:restangular) to show validation errors in a **Toast message**. Of course, there are [API test helpers](doc:api-test-helpers) that are provided so you can easily assert the correct response macro is being used (also makes sure your return types are consistent). For example `->seeApiSuccess()`, `->seeValidationError()`, `->seeApiError(401)`. [block:callout] { "type": "warning", "title": "Validation response", "body": "If you plan on modifying the *error* response macro, you'd need to apply a fix in `App\\Http\\Controllers\\controller.php` which is the base controller." } [/block] > Prefer a blogpost? > [https://blog.jadjoubran.io/2016/03/27/laravel-response-macros-api/](https://blog.jadjoubran.io/2016/03/27/laravel-response-macros-api/)