Rectangle 27 0

nginx Laravel angularJS CORS using barryvdhlaravel cors?


public function __construct()
    {
        $this->afterFilter(function(){

            header('Access-Control-Allow-Origin: *');

        });
    }

And it works like a boss :)

However, I change the code little bit (I don't really know why $response->headers->set(); not working. Instead, I added this to my controller :

It suggests here that you should use the $response->headers->set(); call instead of header(). I don't think using header() is really appropriate in combination with the response objects from Symfony/Laravel. See this article: acairns.co.uk/2013/01/routing-and-cors-with-laravel-4 (But not sure that returning the response is required, as it stop further afterFilters..)

Note
Rectangle 27 0

nginx Laravel angularJS CORS using barryvdhlaravel cors?


Route::filter('access-control', function($route, $request, $response)
{
    $response->headers->set('Access-Control-Allow-Origin', '*');
});
Route::when('api/*', 'access-control');
public function __construct()
{
    $this->afterFilter('access-control');
}

A more Laravel way of setting the header would be to define a Route filter and modify the response there.

I recently update my laravel-cors package so you might want to check it again. But setting the Access-Control-Allow-Origin: * header directly (with the response object) also works, it just doesn't check for allowed hosts, headers and methods etc.

I would avoid using the header() function directly. Also, you are setting the headers both in Nginx and in Laravel, one would suffice.

Then you can set the afterFilter in your Controller, or attach it to a group of routes in your routes.php file.

or apply it when it matches a pattern:

Note
Rectangle 27 0

nginx Laravel angularJS CORS using barryvdhlaravel cors?


Route::filter('access-control', function($route, $request, $response)
{
    $response->headers->set('Access-Control-Allow-Origin', '*');
});
Route::when('api/*', 'access-control');
public function __construct()
{
    $this->afterFilter('access-control');
}

A more Laravel way of setting the header would be to define a Route filter and modify the response there.

I recently update my laravel-cors package so you might want to check it again. But setting the Access-Control-Allow-Origin: * header directly (with the response object) also works, it just doesn't check for allowed hosts, headers and methods etc.

I would avoid using the header() function directly. Also, you are setting the headers both in Nginx and in Laravel, one would suffice.

Then you can set the afterFilter in your Controller, or attach it to a group of routes in your routes.php file.

or apply it when it matches a pattern:

Note
Rectangle 27 0

nginx Laravel angularJS CORS using barryvdhlaravel cors?


public function __construct()
    {
        $this->afterFilter(function(){

            header('Access-Control-Allow-Origin: *');

        });
    }

And it works like a boss :)

However, I change the code little bit (I don't really know why $response->headers->set(); not working. Instead, I added this to my controller :

It suggests here that you should use the $response->headers->set(); call instead of header(). I don't think using header() is really appropriate in combination with the response objects from Symfony/Laravel. See this article: acairns.co.uk/2013/01/routing-and-cors-with-laravel-4 (But not sure that returning the response is required, as it stop further afterFilters..)

Note