laravel cors“类cors不存在”错误

时间:2015-10-12 10:00:38

标签: laravel laravel-5 cors

我已按照https://github.com/barryvdh/laravel-cors在我的应用程序上启用CORS。

当我刷新页面(REST端点)时,我收到以下消息: -

Class cors does not exist

我运行了以下命令: -

composer require barryvdh/laravel-cors 0.7.x

并在我的composer.json

中有以下一行
"barryvdh/laravel-cors": "0.7.x"

routes.php文件

Route::group(['middleware' => 'cors'], function($router){
    $router->get('names/{id}', 'NameController@showEmpDetail');
    $router->resource('names', 'NameController');
});

我猜这个地方我需要调用包,比如使用包等。但是我找不到任何结果来帮助我。

请帮忙。

由于

5 个答案:

答案 0 :(得分:8)

尝试添加

 'cors' => \Barryvdh\Cors\Middleware\HandleCors::class,

 /app/Http/Kernel.php $routeMiddleware

答案 1 :(得分:6)

https://github.com/barryvdh/laravel-cors的readme.md错过了Laravel 5的几个步骤。以下是完整的说明:

  • composer require barryvdh/laravel-cors 0.7.x
  • php artisan vendor:publish --provider="Barryvdh\Cors\ServiceProvider"或将vendor/barryvdh/laravel-cors/config/cors.php复制到config/cors.php
  • config / app.php - > 'providers':Barryvdh\Cors\ServiceProvider::class
  • app / Http / Kernel.php - > $ routeMiddleware:'cors' => \Barryvdh\Cors\Middleware\HandleCors::class
  • php artisan cache:clear(< - 如果需要)

现在,cors中间件将适用于您的路线:

Route::group(['middleware' => 'cors'], function(Router $router){
    $router->get('api', 'ApiController@index'); // <- your route here
});

答案 2 :(得分:1)

只需从app / Http / kernel.php中受保护的$ middlewareGroups中删除cors

答案 3 :(得分:0)

您需要在终端

中运行此命令
php artisan vendor:publish --provider="Barryvdh\Cors\ServiceProvider"

答案 4 :(得分:0)

更改app \ Providers \ RouteServiceProvider.php中的mapApiRoutes()方法,不要放置&#39; cors&#39;在前缀功能中,只需添加&#39; api&#39;作为参数

protected function mapApiRoutes()
{
    Route::prefix('api')
         ->middleware('api')
         ->namespace($this->namespace)
         ->group(base_path('routes/api.php'));
}