From fe1fe78903e5f83c4a5acd937cfa01ce8383df60 Mon Sep 17 00:00:00 2001 From: Dipesh Sukhia Date: Tue, 26 May 2020 19:31:00 +0530 Subject: [PATCH 1/2] add json respons header --- config/config.php | 2 ++ src/LaravelApiGeneratorServiceProvider.php | 5 +++++ src/Middleware/ApiHeaderInject.php | 23 ++++++++++++++++++++++ 3 files changed, 30 insertions(+) create mode 100644 src/Middleware/ApiHeaderInject.php diff --git a/config/config.php b/config/config.php index b80c4c5..c7a89f4 100644 --- a/config/config.php +++ b/config/config.php @@ -15,4 +15,6 @@ */ 'model_directory_path' => 'app', + 'allow_cross_origin' => env('API_ALLOW_CROSS_ORIGIN', false), + 'json_response' => env('API_JSON_RESPONSE', true), ]; diff --git a/src/LaravelApiGeneratorServiceProvider.php b/src/LaravelApiGeneratorServiceProvider.php index 11bb31f..c8840ec 100644 --- a/src/LaravelApiGeneratorServiceProvider.php +++ b/src/LaravelApiGeneratorServiceProvider.php @@ -59,5 +59,10 @@ public function register() $this->app->singleton('laravel-api-generator', function (string $model) { return new LaravelApiGenerator($model); }); + + $this->app['router']->middleware('ApiHeaderInject', 'Bhavingajjar\LaravelApiGenerator\Middleware\ApiHeaderInject'); + + $this->app['router']->aliasMiddleware('ApiHeaderInject', \Bhavingajjar\LaravelApiGenerator\Middleware\ApiHeaderInject::class); + $this->app['router']->pushMiddlewareToGroup('api', \Bhavingajjar\LaravelApiGenerator\Middleware\ApiHeaderInject::class); } } diff --git a/src/Middleware/ApiHeaderInject.php b/src/Middleware/ApiHeaderInject.php new file mode 100644 index 0000000..884d3d6 --- /dev/null +++ b/src/Middleware/ApiHeaderInject.php @@ -0,0 +1,23 @@ +headers('Content-Type', 'application/json') + ->header('Accept', 'application/json'); + } + if(config('laravel-api-generator.allow_cross_origin')) { + $response = $response->header('Access-Control-Allow-Origin', '*') + ->header('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS'); + } + return $response; + } +} From f2f6e7b8ef9de135d7db82aa07ee47bf1b4f6c84 Mon Sep 17 00:00:00 2001 From: Dipesh Sukhia Date: Tue, 26 May 2020 21:10:59 +0530 Subject: [PATCH 2/2] add cross origin support and content type json support --- README.md | 11 ++++++++++- src/Middleware/ApiHeaderInject.php | 15 +++++++++------ 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 2303c04..f731ce6 100644 --- a/README.md +++ b/README.md @@ -13,13 +13,22 @@ This package is used to generate laravel api with Resources You can install the package via composer: ```bash -composer require bhavingajjar/laravel-api-generator --dev +composer require bhavingajjar/laravel-api-generator ``` ## Publish Configuration File ```bash php artisan vendor:publish --provider="Bhavingajjar\LaravelApiGenerator\LaravelApiGeneratorServiceProvider" --tag="config" + +Next, if you plan for cross origin support, you should add middleware to your api middleware group within your app/Http/Kernel.php file: +'ApiHeaderInject' + +add in env +for allow cross origin support +API_ALLOW_CROSS_ORIGIN = true +for json content type +API_JSON_RESPONSE = true ``` ## Usage diff --git a/src/Middleware/ApiHeaderInject.php b/src/Middleware/ApiHeaderInject.php index 884d3d6..3178399 100644 --- a/src/Middleware/ApiHeaderInject.php +++ b/src/Middleware/ApiHeaderInject.php @@ -9,15 +9,18 @@ class ApiHeaderInject { public function handle($request, Closure $next) { - $response = $next($request); if(config('laravel-api-generator.json_response')) { - $response = $response->headers('Content-Type', 'application/json') - ->header('Accept', 'application/json'); + $request->headers->add([ + 'Accept'=>'application/json', + 'Content-Type'=>'application/json' + ]); } if(config('laravel-api-generator.allow_cross_origin')) { - $response = $response->header('Access-Control-Allow-Origin', '*') - ->header('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS'); + $request->headers->add([ + 'Access-Control-Allow-Origin' => '*', + 'Access-Control-Allow-Methods' => 'GET, POST, PUT, DELETE, OPTIONS' + ]); } - return $response; + return $next($request); } }