├── .gitignore ├── changelog.md ├── composer.json ├── config └── lara-json-response.php ├── contributing.md ├── license.md ├── phpunit.xml ├── readme.md └── src ├── Facades └── LaraJsonResponse.php ├── Helpers └── functions.php ├── LaraJsonResponse.php └── LaraJsonResponseServiceProvider.php /.gitignore: -------------------------------------------------------------------------------- 1 | # Created by .ignore support plugin (hsz.mobi) 2 | /vendor 3 | /.idea -------------------------------------------------------------------------------- /changelog.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | 3 | All notable changes to `LaraJsonResponse` will be documented in this file. 4 | 5 | ##### Version 1.0.0 6 | 7 | ###### Added 8 | - Everything 9 | 10 | ##### Version 1.0.1 11 | 12 | ###### Added 13 | - git ignore file 14 | 15 | ###### Removed 16 | - removed unnecessary files from project 17 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "djurovicigoor/larajsonresponse", 3 | "description": "Laravel API wrapper for returning JSON response.", 4 | "license": "MIT", 5 | "authors": [ 6 | { 7 | "name": "Djurovic Igor", 8 | "email": "djurovic.igoor@gmail.com", 9 | "homepage": "https://djurovic.dev" 10 | } 11 | ], 12 | "homepage": "https://github.com/djurovicigoor/larajsonresponse", 13 | "keywords": ["Laravel", "LaraJsonResponse"], 14 | "require": { 15 | }, 16 | "require-dev": { 17 | }, 18 | "autoload": { 19 | "psr-4": { 20 | "DjurovicIgoor\\LaraJsonResponse\\": "src/" 21 | } , 22 | "files": [ 23 | "src/Helpers/functions.php" 24 | ] 25 | }, 26 | "autoload-dev": { 27 | "psr-4": { 28 | "DjurovicIgoor\\LaraJsonResponse\\Tests\\": "tests" 29 | } 30 | }, 31 | "extra": { 32 | "laravel": { 33 | "providers": [ 34 | "DjurovicIgoor\\LaraJsonResponse\\LaraJsonResponseServiceProvider" 35 | ], 36 | "aliases": { 37 | "LaraJsonResponse": "DjurovicIgoor\\LaraJsonResponse\\Facades\\LaraJsonResponse" 38 | } 39 | } 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /config/lara-json-response.php: -------------------------------------------------------------------------------- 1 | 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. -------------------------------------------------------------------------------- /phpunit.xml: -------------------------------------------------------------------------------- 1 | 2 | 12 | 13 | 14 | ./tests/ 15 | 16 | 17 | 18 | 19 | src/ 20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # LaraJsonResponse 2 | 3 | [![Latest Version on Packagist](https://img.shields.io/packagist/v/djurovicigoor/larajsonresponse.svg?style=for-the-badge)](https://packagist.org/packages/djurovicigoor/larajsonresponse) 4 | ![Total Downloads](https://img.shields.io/packagist/dt/djurovicigoor/larajsonresponse.svg?style=for-the-badge) 5 | [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg?style=for-the-badge)](https://opensource.org/licenses/MIT) 6 | ![Scrutinizer code quality (GitHub/Bitbucket)](https://img.shields.io/scrutinizer/quality/g/djurovicigoor/lara-json-response/master.svg?style=for-the-badge) 7 | ![Scrutinizer build (GitHub/Bitbucket)](https://img.shields.io/scrutinizer/build/g/djurovicigoor/lara-json-response/master.svg?style=for-the-badge) 8 | 9 | Laravel API wrapper for returning JSON response. 10 | 11 | ## Installation 12 | 13 | Via Composer 14 | 15 | ``` bash 16 | $ composer require djurovicigoor/larajsonresponse 17 | ``` 18 | 19 | ## Usage 20 | 21 | The base method is **laraResponse()**; 22 | ``` bash 23 | return laraResponse(); 24 | ``` 25 | JSON response of this method will be: 26 | ``` bash 27 | { 28 | "code": 200, 29 | "message": null, 30 | "data": null, 31 | "error": null 32 | } 33 | ``` 34 | If you want to return success JSON response with data and status code 200, you have to use **laraResponse($message , $data)->success()** 35 | ``` bash 36 | $data = ['name' => 'John Doe', 'location' => 'unknown' , 'age' => 24]; 37 | 38 | return laraResponse("My message." , $data)->success(); 39 | ``` 40 | JSON response of this method will be: 41 | ``` bash 42 | { 43 | "code": 200, 44 | "message": "My message.", 45 | "data": { 46 | "name": "John Doe", 47 | "location": "unknown", 48 | "age": 24 49 | }, 50 | "error": null 51 | } 52 | ``` 53 | If you want to return error JSON response with message, ERROR and status code 400, you have to use **laraResponse($message , NULL , $error)->error()** 54 | ``` bash 55 | return laraResponse('My message.' , NULL , "TYPE_OF_ERROR")->error(); 56 | ``` 57 | JSON response of this method will be: 58 | ``` bash 59 | { 60 | "code": 400, 61 | "message": "My message.", 62 | "data": null, 63 | "error": "TYPE_OF_ERROR" 64 | } 65 | ``` 66 | If you want to return unauthorized JSON response response with 401 status code, you have to use **laraResponse($message)->unAuthorized()** 67 | ``` bash 68 | return laraResponse('This action is unauthorized.')->unAuthorized(); 69 | ``` 70 | JSON response of this method will be: 71 | ``` bash 72 | { 73 | "code": 401, 74 | "message": "This action is unauthorized.", 75 | "data": null, 76 | "error": null 77 | } 78 | ``` 79 | If you want to return forbidden JSON response response with 403 status code, you have to use **laraResponse($message)->forbidden()** 80 | ``` bash 81 | return laraResponse('Forbidden!')->forbidden(); 82 | ``` 83 | JSON response of this method will be: 84 | ``` bash 85 | { 86 | "code": 403, 87 | "message": "Forbidden!", 88 | "data": null, 89 | "error": null 90 | } 91 | ``` 92 | If you want to return not found JSON response with 404 status code , you have to use **laraResponse($message)->notFound()** 93 | ``` bash 94 | return laraResponse('Not Found!')->notFound(); 95 | ``` 96 | JSON response of this method will be: 97 | ``` bash 98 | { 99 | "code": 404, 100 | "message": "Not Found!", 101 | "data": null, 102 | "error": null 103 | } 104 | ``` 105 | And finaly you can use **laraResponse($message)->customCode($yourCustomCode)** to return JSON response with yout custom code; 106 | ``` bash 107 | return laraResponse('My custom status code!')->customCode($yourCustomCode); 108 | ``` 109 | JSON response of this method will be: 110 | ``` bash 111 | { 112 | "code": $yourCustomCode, 113 | "message": "My custom status code!", 114 | "data": null, 115 | "error": null 116 | } 117 | ``` 118 | 119 | ## Change log 120 | 121 | Please see the [changelog](changelog.md) for more information on what has changed recently. 122 | 123 | ## Contributing 124 | 125 | Please see [contributing.md](contributing.md) for details and a todolist. 126 | 127 | ## Security 128 | 129 | If you discover any security related issues, please email djurovic.igoor@gmail.com instead of using the issue tracker. 130 | 131 | ## Donate 132 | 133 | If you found this project helpful or you learned something from the source code and want to appreciate 134 | 135 | - [PayPal](https://paypal.me/djurovicigoor?locale.x=en_US) 136 | 137 | ## Credits 138 | 139 | - [Djurovic Igor][link-author] 140 | 141 | ## License 142 | 143 | MIT. Please see the [license file](license.md) for more information. 144 | 145 | [ico-version]: https://img.shields.io/packagist/v/djurovicigoor/larajsonresponse.svg?style=flat-square 146 | [ico-downloads]: https://img.shields.io/packagist/dt/djurovicigoor/larajsonresponse.svg?style=flat-square 147 | [ico-travis]: https://img.shields.io/travis/djurovicigoor/larajsonresponse/master.svg?style=flat-square 148 | [ico-styleci]: https://styleci.io/repos/12345678/shield 149 | 150 | [link-packagist]: https://packagist.org/packages/djurovicigoor/larajsonresponse 151 | [link-downloads]: https://packagist.org/packages/djurovicigoor/larajsonresponse 152 | [link-travis]: https://travis-ci.org/djurovicigoor/larajsonresponse 153 | [link-styleci]: https://styleci.io/repos/12345678 154 | [link-author]: https://github.com/djurovicigoor 155 | [link-contributors]: ../../contributors] 156 | -------------------------------------------------------------------------------- /src/Facades/LaraJsonResponse.php: -------------------------------------------------------------------------------- 1 | success(); 27 | } 28 | 29 | return $laraResponse; 30 | } 31 | } -------------------------------------------------------------------------------- /src/LaraJsonResponse.php: -------------------------------------------------------------------------------- 1 | message = $message; 24 | $this->data = $data; 25 | $this->error = $error; 26 | $this->code = 200; 27 | } 28 | 29 | /** 30 | * @return \Illuminate\Http\JsonResponse 31 | */ 32 | public function success() { 33 | 34 | return $this->globalResponse(200); 35 | } 36 | 37 | /** 38 | * @return \Illuminate\Http\JsonResponse 39 | */ 40 | public function error() { 41 | 42 | return $this->globalResponse(400); 43 | } 44 | 45 | /** 46 | * @return \Illuminate\Http\JsonResponse 47 | */ 48 | public function unAuthorized() { 49 | 50 | return $this->globalResponse(401); 51 | } 52 | 53 | /** 54 | * @return \Illuminate\Http\JsonResponse 55 | */ 56 | public function forbidden() { 57 | 58 | return $this->globalResponse(403); 59 | } 60 | 61 | /** 62 | * @return \Illuminate\Http\JsonResponse 63 | */ 64 | public function notFound() { 65 | 66 | return $this->globalResponse(404); 67 | } 68 | 69 | /** 70 | * @return \Illuminate\Http\JsonResponse 71 | */ 72 | public function validationError() { 73 | 74 | return $this->globalResponse(422); 75 | } 76 | 77 | /** 78 | * @param $code 79 | * 80 | * @return \Illuminate\Http\JsonResponse 81 | */ 82 | public function customCode($code) { 83 | 84 | return $this->globalResponse($code); 85 | } 86 | 87 | /** 88 | * @param null $code 89 | * 90 | * @return \Illuminate\Http\JsonResponse 91 | */ 92 | public function globalResponse($code = NULL) { 93 | 94 | return Response::json([ 95 | 'code' => !is_null($code) ? $code : $this->code, 96 | 'message' => $this->message, 97 | 'data' => $this->data, 98 | 'error' => $this->error, 99 | ], !is_null($code) ? $code : $this->code); 100 | } 101 | } -------------------------------------------------------------------------------- /src/LaraJsonResponseServiceProvider.php: -------------------------------------------------------------------------------- 1 | registerHelpers(); 16 | } 17 | 18 | /** 19 | * Register any package services. 20 | * @return void 21 | */ 22 | public function register() { 23 | 24 | // Register the service the package provides. 25 | $this->app->singleton('laraResponse', function($app) { 26 | 27 | return new LaraJsonResponse; 28 | }); 29 | } 30 | 31 | /** 32 | * Get the services provided by the provider. 33 | * @return array 34 | */ 35 | public function provides() { 36 | 37 | return ['larajsonresponse']; 38 | } 39 | 40 | /** 41 | * Register helpers file 42 | */ 43 | public function registerHelpers() { 44 | 45 | // Load the helpers in app/Http/helpers.php 46 | if (file_exists($file = __DIR__ . '/Helpers/functions.php')) { 47 | require $file; 48 | } 49 | } 50 | } --------------------------------------------------------------------------------