Skip to content

Commit

Permalink
Merge pull request #3 from dipeshsukhia/master
Browse files Browse the repository at this point in the history
add cross origin support and content type json support
  • Loading branch information
bhavingajjar authored May 27, 2020
2 parents 1190d4c + f2f6e7b commit 987f57e
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 1 deletion.
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,6 @@
*/
'model_directory_path' => 'app',

'allow_cross_origin' => env('API_ALLOW_CROSS_ORIGIN', false),
'json_response' => env('API_JSON_RESPONSE', true),
];
5 changes: 5 additions & 0 deletions src/LaravelApiGeneratorServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
26 changes: 26 additions & 0 deletions src/Middleware/ApiHeaderInject.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php
namespace Bhavingajjar\LaravelApiGenerator\Middleware;

use Closure;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\Config;

class ApiHeaderInject
{
public function handle($request, Closure $next)
{
if(config('laravel-api-generator.json_response')) {
$request->headers->add([
'Accept'=>'application/json',
'Content-Type'=>'application/json'
]);
}
if(config('laravel-api-generator.allow_cross_origin')) {
$request->headers->add([
'Access-Control-Allow-Origin' => '*',
'Access-Control-Allow-Methods' => 'GET, POST, PUT, DELETE, OPTIONS'
]);
}
return $next($request);
}
}

0 comments on commit 987f57e

Please sign in to comment.