├── config └── http.php ├── src ├── Middlewares │ └── SetHeaderMiddleware.php └── ServiceProvider.php ├── LICENSE ├── composer.json └── README.md /config/http.php: -------------------------------------------------------------------------------- 1 | [ 7 | /* 8 | * This setting is responsible for applying middleware to routes. 9 | * 10 | * You can specify route group names to apply the `SetHeaderMiddleware` 11 | * middleware to them, or specify null to apply it to all routes. 12 | * 13 | * For example, 14 | * 15 | * json => null 16 | * json => 'api' 17 | * json => ['api', 'web'] 18 | */ 19 | 'json' => null, 20 | ], 21 | ]; 22 | -------------------------------------------------------------------------------- /src/Middlewares/SetHeaderMiddleware.php: -------------------------------------------------------------------------------- 1 | set( 16 | $next($this->set($request)) 17 | ); 18 | } 19 | 20 | protected function set($request) 21 | { 22 | if (! $this->hasHeader($request) || $this->isAsterisk($request)) { 23 | $request->headers->set(Header::ACCEPT, 'application/json'); 24 | } 25 | 26 | return $request; 27 | } 28 | 29 | protected function hasHeader($request): bool 30 | { 31 | return $request->headers->has(Header::ACCEPT); 32 | } 33 | 34 | protected function isAsterisk($request): bool 35 | { 36 | return Str::contains($request->headers->get(Header::ACCEPT, ''), '*/*'); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2021-2025 Andrey Helldar 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. 22 | -------------------------------------------------------------------------------- /src/ServiceProvider.php: -------------------------------------------------------------------------------- 1 | publishConfig(); 19 | 20 | $this->registerMiddleware($this->middlewareGroups()); 21 | } 22 | 23 | public function register(): void 24 | { 25 | $this->registerConfig(); 26 | } 27 | 28 | protected function registerMiddleware(array $groups): void 29 | { 30 | $this->toAll($groups) 31 | ? $this->resolve()->prependMiddleware($this->middleware) 32 | : $this->prependMiddlewareToGroups($this->middleware, $groups); 33 | } 34 | 35 | protected function prependMiddlewareToGroups(string $middleware, array $groups): void 36 | { 37 | foreach ($groups as $group) { 38 | $this->resolve()->prependMiddlewareToGroup($group, $middleware); 39 | } 40 | } 41 | 42 | protected function toAll(array $groups): bool 43 | { 44 | return empty($groups); 45 | } 46 | 47 | protected function middlewareGroups(): array 48 | { 49 | return array_filter(Arr::wrap( 50 | $this->app['config']->get('http.response.json') 51 | )); 52 | } 53 | 54 | protected function publishConfig(): void 55 | { 56 | $this->publishes([ 57 | __DIR__ . '/../config/http.php' => $this->app->configPath('http.php'), 58 | ]); 59 | } 60 | 61 | protected function registerConfig(): void 62 | { 63 | $this->mergeConfigFrom( 64 | __DIR__ . '/../config/http.php', 65 | 'http' 66 | ); 67 | } 68 | 69 | protected function resolve(): Kernel 70 | { 71 | return $this->app->make(Kernel::class); 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "dragon-code/laravel-json-response", 3 | "description": "Automatically always return a response in JSON format", 4 | "license": "MIT", 5 | "type": "library", 6 | "keywords": [ 7 | "api", 8 | "json", 9 | "laravel", 10 | "response", 11 | "dragon-code", 12 | "dragon", 13 | "andrey-helldar" 14 | ], 15 | "authors": [ 16 | { 17 | "name": "Andrey Helldar", 18 | "email": "helldar@dragon-code.pro", 19 | "homepage": "https://dragon-code.pro" 20 | } 21 | ], 22 | "support": { 23 | "issues": "https://github.com/TheDragonCode/laravel-json-response/issues", 24 | "source": "https://github.com/TheDragonCode/laravel-json-response" 25 | }, 26 | "funding": [ 27 | { 28 | "type": "boosty", 29 | "url": "https://boosty.to/dragon-code" 30 | }, 31 | { 32 | "type": "yoomoney", 33 | "url": "https://yoomoney.ru/to/410012608840929" 34 | } 35 | ], 36 | "require": { 37 | "php": "^8.1", 38 | "illuminate/contracts": "^10.0 || ^11.0 || ^12.0", 39 | "illuminate/http": "^10.0 || ^11.0 || ^12.0", 40 | "illuminate/support": "^10.0 || ^11.0 || ^12.0", 41 | "lmc/http-constants": "^1.2", 42 | "symfony/http-foundation": "^6.0 || ^7.0" 43 | }, 44 | "require-dev": { 45 | "orchestra/testbench": "^8.0 || ^9.0 || ^10.0", 46 | "phpunit/phpunit": "^10.0 || ^11.0 || ^12.0" 47 | }, 48 | "conflict": { 49 | "andrey-helldar/laravel-json-response": "*" 50 | }, 51 | "minimum-stability": "stable", 52 | "prefer-stable": true, 53 | "autoload": { 54 | "psr-4": { 55 | "DragonCode\\LaravelJsonResponse\\": "src" 56 | } 57 | }, 58 | "autoload-dev": { 59 | "psr-4": { 60 | "Tests\\": "tests" 61 | } 62 | }, 63 | "config": { 64 | "allow-plugins": { 65 | "dragon-code/codestyler": true, 66 | "ergebnis/composer-normalize": true, 67 | "friendsofphp/php-cs-fixer": true, 68 | "symfony/thanks": true 69 | }, 70 | "preferred-install": "dist", 71 | "sort-packages": true 72 | }, 73 | "extra": { 74 | "laravel": { 75 | "providers": [ 76 | "DragonCode\\LaravelJsonResponse\\ServiceProvider" 77 | ] 78 | } 79 | } 80 | } 81 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Json Response for Laravel 2 | 3 | Json Response for Laravel 4 | 5 | [![Stable Version][badge_stable]][link_packagist] 6 | [![Total Downloads][badge_downloads]][link_packagist] 7 | [![Github Workflow Status][badge_build]][link_build] 8 | [![License][badge_license]][link_license] 9 | 10 | > Automatically always return a response in JSON format 11 | 12 | ## Installation 13 | 14 | ### Compatibility 15 | 16 | | PHP | Laravel | Json Response | 17 | |-----------------------------------|--------------------------|---------------| 18 | | 8.1, 8.2, 8.3, 8.4 | 10.x, 11.x, 12.x | `^3.0` | 19 | | 7.3, 7.4, 8.0, 8.1, 8.2, 8.3, 8.4 | 6.x, 7.x, 8.x, 9.x, 10.x | `^2.0` | 20 | 21 | 22 | To get the latest version of `Laravel Json Response`, simply require the project using [Composer](https://getcomposer.org): 23 | 24 | ```bash 25 | composer require dragon-code/laravel-json-response 26 | ``` 27 | 28 | Or manually update `require` block of `composer.json` and run `composer update`. 29 | 30 | ```json 31 | { 32 | "require": { 33 | "dragon-code/laravel-json-response": "^3.0" 34 | } 35 | } 36 | ``` 37 | 38 | ## Using 39 | 40 | After you've installed the package via composer, you're done. There's no step two. 41 | 42 | This package will automatically register the `DragonCode\LaravelJsonResponse\Middlewares\SetHeaderMiddleware` middleware in the `web` and `api` groups, if they exist. The 43 | middleware will add a header `Accept` that will effectively convert all responses to JSON format. This header will apply to all responses. 44 | 45 | > If you need to redefine the header for specific groups of routes, you can do this by changing the [`settings`](config/http.php). 46 | 47 | 48 | [badge_build]: https://img.shields.io/github/actions/workflow/status/TheDragonCode/laravel-json-response/tests.yml?style=flat-square 49 | 50 | [badge_downloads]: https://img.shields.io/packagist/dt/dragon-code/laravel-json-response.svg?style=flat-square 51 | 52 | [badge_license]: https://img.shields.io/packagist/l/dragon-code/laravel-json-response.svg?style=flat-square 53 | 54 | [badge_stable]: https://img.shields.io/github/v/release/TheDragonCode/laravel-json-response?label=stable&style=flat-square 55 | 56 | [link_build]: https://github.com/TheDragonCode/laravel-json-response/actions 57 | 58 | [link_license]: LICENSE 59 | 60 | [link_packagist]: https://packagist.org/packages/dragon-code/laravel-json-response 61 | --------------------------------------------------------------------------------