Skip to content
This repository has been archived by the owner on Feb 16, 2023. It is now read-only.

Commit

Permalink
Add L5 middleware / support
Browse files Browse the repository at this point in the history
  • Loading branch information
barryvdh committed Nov 24, 2014
1 parent 2255dfc commit 402de0f
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 8 deletions.
4 changes: 4 additions & 0 deletions readme.md
Expand Up @@ -75,6 +75,10 @@ Require the `barryvdh/laravel-cors` package in your composer.json and update you
Add the CorsServiceProvider to your app/config/app.php providers array:

'Barryvdh\Cors\CorsServiceProvider',
For Laravel 5.x, you need to add the Middleware to your App Kernel:

'Barryvdh\Cors\Middleware\HandleCors',

Publish the config file to create your own configuration:

Expand Down
31 changes: 23 additions & 8 deletions src/CorsServiceProvider.php
@@ -1,8 +1,7 @@
<?php namespace Barryvdh\Cors;


use Asm89\Stack\CorsService;
use Illuminate\Support\Str;
use ReflectionClass;
use Illuminate\Support\ServiceProvider;
use Illuminate\Http\Request;

Expand Down Expand Up @@ -34,12 +33,18 @@ public function register()
{
/** @var \Illuminate\Http\Request $request */
$request = $this->app['request'];
if (!$request->headers->has('Origin') || $request->headers->get('Origin') == $request->getSchemeAndHttpHost()) {
return;
}

$this->app['config']->package('barryvdh/laravel-cors', realpath(__DIR__ . '/config'));
$this->app->middleware('Asm89\Stack\Cors', array($this->getOptions($request)));

if ($this->checkVersion('5.0-dev', '<')) {
if ($request->headers->has('Origin') && $request->headers->get('Origin') !== $request->getSchemeAndHttpHost()) {
$this->app->middleware('Asm89\Stack\Cors', array($this->getOptions($request)));
}
} else {
$this->app->bind('Asm89\Stack\CorsService', function() use($request){
return new CorsService($this->getOptions($request));
});
}
}

/**
Expand Down Expand Up @@ -102,8 +107,18 @@ protected function normalizeOptions($options)
}
}
return $options;

}

/**
* Compare the current Laravel version
*
* @param $version
* @param null $operator
* @return mixed
*/
protected function checkVersion($version, $operator = null)
{
return version_compare($this->app->version(), $version, $operator);
}

}
}
47 changes: 47 additions & 0 deletions src/Middleware/HandleCors.php
@@ -0,0 +1,47 @@
<?php namespace Barryvdh\Cors\Middleware;

use Closure;
use Asm89\Stack\CorsService;
use Illuminate\Contracts\Routing\Middleware;
use Symfony\Component\HttpFoundation\Response;

class HandleCors implements Middleware {

/**
* @param CorsService $cors
*/
public function __construct(CorsService $cors)
{
$this->cors = $cors;
}

/**
* Handle an incoming request. Based on Asm89\Stack\Cors by asm89
* @see https://github.com/asm89/stack-cors/blob/master/src/Asm89/Stack/Cors.php
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
if (
! $this->cors->isCorsRequest($request)
|| $request->headers->get('Origin') == $request->getSchemeAndHttpHost()
) {
return $next($request);
}

if ($this->cors->isPreflightRequest($request)) {
return $this->cors->handlePreflightRequest($request);
}

if ( ! $this->cors->isActualRequestAllowed($request)) {
return new Response('Not allowed.', 403);
}

$response = $next($request);

return $this->cors->addActualRequestHeaders($response, $request);
}
}

0 comments on commit 402de0f

Please sign in to comment.