Skip to content

Commit 987f57e

Browse files
authored
Merge pull request #3 from dipeshsukhia/master
add cross origin support and content type json support
2 parents 1190d4c + f2f6e7b commit 987f57e

File tree

4 files changed

+43
-1
lines changed

4 files changed

+43
-1
lines changed

README.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,22 @@ This package is used to generate laravel api with Resources
1313
You can install the package via composer:
1414

1515
```bash
16-
composer require bhavingajjar/laravel-api-generator --dev
16+
composer require bhavingajjar/laravel-api-generator
1717
```
1818

1919
## Publish Configuration File
2020

2121
```bash
2222
php artisan vendor:publish --provider="Bhavingajjar\LaravelApiGenerator\LaravelApiGeneratorServiceProvider" --tag="config"
23+
24+
Next, if you plan for cross origin support, you should add middleware to your api middleware group within your app/Http/Kernel.php file:
25+
'ApiHeaderInject'
26+
27+
add in env
28+
for allow cross origin support
29+
API_ALLOW_CROSS_ORIGIN = true
30+
for json content type
31+
API_JSON_RESPONSE = true
2332
```
2433
2534
## Usage

config/config.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,6 @@
1515
*/
1616
'model_directory_path' => 'app',
1717

18+
'allow_cross_origin' => env('API_ALLOW_CROSS_ORIGIN', false),
19+
'json_response' => env('API_JSON_RESPONSE', true),
1820
];

src/LaravelApiGeneratorServiceProvider.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,5 +59,10 @@ public function register()
5959
$this->app->singleton('laravel-api-generator', function (string $model) {
6060
return new LaravelApiGenerator($model);
6161
});
62+
63+
$this->app['router']->middleware('ApiHeaderInject', 'Bhavingajjar\LaravelApiGenerator\Middleware\ApiHeaderInject');
64+
65+
$this->app['router']->aliasMiddleware('ApiHeaderInject', \Bhavingajjar\LaravelApiGenerator\Middleware\ApiHeaderInject::class);
66+
$this->app['router']->pushMiddlewareToGroup('api', \Bhavingajjar\LaravelApiGenerator\Middleware\ApiHeaderInject::class);
6267
}
6368
}

src/Middleware/ApiHeaderInject.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
namespace Bhavingajjar\LaravelApiGenerator\Middleware;
3+
4+
use Closure;
5+
use Illuminate\Http\Response;
6+
use Illuminate\Support\Facades\Config;
7+
8+
class ApiHeaderInject
9+
{
10+
public function handle($request, Closure $next)
11+
{
12+
if(config('laravel-api-generator.json_response')) {
13+
$request->headers->add([
14+
'Accept'=>'application/json',
15+
'Content-Type'=>'application/json'
16+
]);
17+
}
18+
if(config('laravel-api-generator.allow_cross_origin')) {
19+
$request->headers->add([
20+
'Access-Control-Allow-Origin' => '*',
21+
'Access-Control-Allow-Methods' => 'GET, POST, PUT, DELETE, OPTIONS'
22+
]);
23+
}
24+
return $next($request);
25+
}
26+
}

0 commit comments

Comments
 (0)