Just the route shown: $router->group(['prefix' => 'api', 'middleware' => 'cors'], function ($router) { $router->get('/test', 'MyController@myMethod'); });
No, unfortunately not. I have solved it temporarily by adding the CORS middleware to the $middeware array instead of $routedMiddleware. This does however mean it's applied throughout the application. Does anyone have a better solution?
Hey, I tried to achieve cors in my laravel (5.2) application. I used barryvdh/laravel-cors and had some issues until I read a laracasts thread from a year ago. @Barryvdh suggested the order in wich middleware is called is important. So I modified my kernel and routes to this:
//App\Http\Kernel.php
protected $routeMiddleware = [
'cors' => \Barryvdh\Cors\HandleCors::class,
'auth' => \App\Http\Middleware\Authenticate::class,
'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
'can' => \Illuminate\Foundation\Http\Middleware\Authorize::class,
'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
'csrf' => \App\Http\Middleware\VerifyCsrfToken::class,
];
I added HandleCors at the top of the routeMiddleware array (even if I am pretty sure this does not change anything)
//Routes.php
Route::group(['prefix' => 'api', 'middleware' => ['cors', 'api'], 'namespace' => 'API'], function()
{
//API Routes
});
In my route group I made sure cors is called before using the other api routes. This worked for me.
Initially my ember app couldn't connect:
Failed to load resource: Origin http://localhost:4200 is not allowed by Access-Control-Allow-Origin.
And now I get this
InvalidCharacterError: DOM Exception 5
wich is fine, because at least cors seems to work properly.
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community