I've got it working today using a before filter and an Route::options('{all}').
Both call the same controller method:
use Illuminate\Routing\Controller;
use Request;
use Response;
use Config;
class Cors extends Controller {
public function handle() {
$validDomain = Config::get('app.cors.allow_origin');
$domain = Request::server('HTTP_HOST');
if ($validDomain != '*' && $validDomain != $domain) {
return Response::make('Access denied', 403);
}
// Enable CORS
header('Access-Control-Allow-Origin: ' . $validDomain);
header('Access-Control-Allow-Credentials: true');
if (Request::getMethod() == "OPTIONS") {
// The client-side application can set only headers allowed in Access-Control-Allow-Headers
$headers = [
'Access-Control-Allow-Methods'=> 'POST, GET, OPTIONS, PUT, DELETE',
'Access-Control-Allow-Headers'=> 'X-Requested-With, Content-Type, X-Auth-Token, Origin, Authorization'
];
return Response::make('You are connected to the API', 200, $headers);
}
}
}