fruitcake / laravel-cors

Adds CORS (Cross-Origin Resource Sharing) headers support in your Laravel application
MIT License
6.27k stars 614 forks source link

Laravel 5.2.24 "Class cors does not exists" #109

Closed dominikveils closed 6 years ago

dominikveils commented 8 years ago

After updating my project to 5.2.24 I'm getting this error: "Class cors does not exists"

I've used the 'cors' middleware in my Kernel.php

PS After changing it to \Barryvdh\Cors\HandleCors::class the error is disappeared, so I think the problem with the named middleware: 'cors'.

nerijunior commented 8 years ago

I'm having the same problem. +1

dominikveils commented 8 years ago

@nerijunior as a temp solution you can change 'cors' to \Barryvdh\Cors\HandleCors::class, it will work.

ghost commented 8 years ago

+1

MikeAlhayek commented 7 years ago

I don't think its a bug. It may be how you upgraded.

I had the same error. In my case, I had the following line in my Kernel.php file

'cors' => \App\Http\Middleware\Cors::class,

However, the class App\Http\Middleware\Cors did not exists.

After creating the following class everything worked.

<?php

namespace App\Http\Middleware;

use Closure;

class Cors
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        return $next($request)
            ->header('Access-Control-Allow-Origin', '*')
            ->header('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, PATCH');
    }
}
nerijunior commented 7 years ago

@CrestApps the problem is the OPTIONS preflight request, this simple implementation of CORS middleware do not cover the preflight.

tucq88 commented 7 years ago

I'm having the same problem with Laravel 5.3.30.

JTallis commented 7 years ago

I added the middleware to the global middleware stack which gave me issues when I attempted to use the middleware in my routes. I had to add 'cors' => \Barryvdh\Cors\HandleCors::class, to $routeMiddleware and then I was able to use the middleware cors in my routes. I'm not sure if this is related to the issues people have been having or not.

tucq88 commented 7 years ago

@JTallis So do I. It's quite strange because it's only me who need add cors to routeMiddleware while other colleagues don't (and it works on their side ntw).

MikeAlhayek commented 7 years ago

@tucq88. I think this issue only happened if you add ->middlewear('cors') to your route setup. If you don't then you won't get this issue.

tucq88 commented 7 years ago
\Route::middleware(['cors', 'bindings'])
             ->namespace($this->namespace . '\Api')
             ->prefix('api/v1')
             ->group(base_path('routes/api.php'));

No, I added it in RouteProvider instead. Most strange part is everybody works ok. Only me :'(

MikeAlhayek commented 7 years ago

Look in the kernel.php file for the value representing the cors key. Then, make sure that file exists in your app.

'cors' => \App\Http\Middleware\Cors::class,