laravel cors“类 cors 不存在”错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33078340/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
laravel cors "Class cors does not exist" error
提问by Stacy J
I have followed https://github.com/barryvdh/laravel-corsto enable CORS on my application.
我已按照https://github.com/barryvdh/laravel-cors在我的应用程序上启用 CORS。
When I refresh my page (REST endpoint), I get the following message :-
当我刷新页面(REST 端点)时,我收到以下消息:-
Class cors does not exist
I ran the following command :-
我运行了以下命令:-
composer require barryvdh/laravel-cors 0.7.x
and have the following line in my composer.json
并在我的 composer.json 中有以下行
"barryvdh/laravel-cors": "0.7.x"
Routes.php
路由.php
Route::group(['middleware' => 'cors'], function($router){
$router->get('names/{id}', 'NameController@showEmpDetail');
$router->resource('names', 'NameController');
});
I am guessing that somewhere I'll need to call the package, like use the package etc..but I am not finding any result to help me out.
我猜我需要在某个地方调用包,比如使用包等。但我没有找到任何结果来帮助我。
Please help.
请帮忙。
Thanks
谢谢
回答by Syrok
Try to add
尝试添加
'cors' => \Barryvdh\Cors\Middleware\HandleCors::class,
to
到
/app/Http/Kernel.php $routeMiddleware
回答by Zack Morris
The readme.md at https://github.com/barryvdh/laravel-corsis missing a few steps for Laravel 5. Here are the complete instructions:
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"
or copyvendor/barryvdh/laravel-cors/config/cors.php
toconfig/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
(<- if needed)
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 -> '提供者':
Barryvdh\Cors\ServiceProvider::class
- app/Http/Kernel.php -> $routeMiddleware:
'cors' => \Barryvdh\Cors\Middleware\HandleCors::class
php artisan cache:clear
(<- 如果需要)
Now the cors middleware will work for your routes:
现在 cors 中间件将适用于您的路由:
Route::group(['middleware' => 'cors'], function(Router $router){
$router->get('api', 'ApiController@index'); // <- your route here
});
回答by Basit
You need to run this command in terminal
您需要在终端中运行此命令
php artisan vendor:publish --provider="Barryvdh\Cors\ServiceProvider"
回答by Hervera
Change your mapApiRoutes() method in app\Providers\RouteServiceProvider.php, to this, do not put 'cors' in the prefix function, just put only 'api' as the parameter
在app\Providers\RouteServiceProvider.php 中修改你的mapApiRoutes() 方法,为此,不要在前缀函数中放置'cors',只将'api' 作为参数
protected function mapApiRoutes()
{
Route::prefix('api')
->middleware('api')
->namespace($this->namespace)
->group(base_path('routes/api.php'));
}