Support the ongoing development of Laravel.io →
posted 8 years ago
Requests
Last updated 2 years ago.
0

What routes are you applying that cors middleware to?

0

Just the route shown: $router->group(['prefix' => 'api', 'middleware' => 'cors'], function ($router) { $router->get('/test', 'MyController@myMethod'); });

0

I am having the same issue

0

Anyone figure this out? Just run into exactly the same issue...

0

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?

0

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.

Last updated 8 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.