Laravel 5.1 API启用Cors

我一直在寻找一些方法来专门在laravel 5.1上启用cors,我发现了一些库,例如:


https://github.com/neomerx/cors-illuminate


https://github.com/barryvdh/laravel-cors


但是他们都没有专门针对Laravel 5.1的实现教程,我尝试进行配置,但没有用。


如果有人已经在laravel 5.1上实现了CORS,我将非常感谢您的帮助...


哔哔one
浏览 795回答 3
3回答

慕森王

这是我的CORS中间件:<?php namespace App\Http\Middleware;use Closure;class CORS {&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* Handle an incoming request.&nbsp; &nbsp; &nbsp;*&nbsp; &nbsp; &nbsp;* @param&nbsp; \Illuminate\Http\Request&nbsp; $request&nbsp; &nbsp; &nbsp;* @param&nbsp; \Closure&nbsp; $next&nbsp; &nbsp; &nbsp;* @return mixed&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; public function handle($request, Closure $next)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; header("Access-Control-Allow-Origin: *");&nbsp; &nbsp; &nbsp; &nbsp; // ALLOW OPTIONS METHOD&nbsp; &nbsp; &nbsp; &nbsp; $headers = [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'Access-Control-Allow-Methods'=> 'POST, GET, OPTIONS, PUT, DELETE',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'Access-Control-Allow-Headers'=> 'Content-Type, X-Auth-Token, Origin'&nbsp; &nbsp; &nbsp; &nbsp; ];&nbsp; &nbsp; &nbsp; &nbsp; if($request->getMethod() == "OPTIONS") {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // The client-side application can set only headers allowed in Access-Control-Allow-Headers&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return Response::make('OK', 200, $headers);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; $response = $next($request);&nbsp; &nbsp; &nbsp; &nbsp; foreach($headers as $key => $value)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $response->header($key, $value);&nbsp; &nbsp; &nbsp; &nbsp; return $response;&nbsp; &nbsp; }}要使用CORS中间件,您必须先在app \ Http \ Kernel.php文件中注册它,如下所示:protected $routeMiddleware = [&nbsp; &nbsp; &nbsp; &nbsp; //other middlewares&nbsp; &nbsp; &nbsp; &nbsp; 'cors' => 'App\Http\Middleware\CORS',&nbsp; &nbsp; ];然后您可以在路线中使用它Route::get('example', array('middleware' => 'cors', 'uses' => 'ExampleController@dummy'));

Smart猫小萌

我总是使用一种简单的方法。只需将以下行添加到\public\index.php文件即可。我认为您不必使用中间件。header('Access-Control-Allow-Origin: *');&nbsp;&nbsp;header('Access-Control-Allow-Methods: GET, PUT, POST, DELETE, OPTIONS');

繁花不似锦

我使用的是Laravel 5.4,不幸的是,尽管接受的答案似乎很好,但对于预检请求(如PUT和DELETE),该OPTIONS请求将在请求之前,在$routeMiddleware数组中指定中间件(并在路由定义文件中使用),除非您也定义一个路由处理程序OPTIONS。这是因为如果没有OPTIONS路由,Laravel将在内部响应该方法而没有CORS标头。简而言之,要么在$middleware数组中定义针对所有请求全局运行的中间件,要么在其中进行操作,$middlewareGroups或者$routeMiddleware为定义路由处理程序OPTIONS。可以这样完成:Route::match(['options', 'put'], '/route', function () {&nbsp; &nbsp; // This will work with the middleware shown in the accepted answer})->middleware('cors');我还出于相同的目的编写了一个中间件,该中间件看起来很相似,但是由于试图更好地配置和处理一系列条件,因此其尺寸更大:<?phpnamespace App\Http\Middleware;use Closure;class Cors{&nbsp; &nbsp; private static $allowedOriginsWhitelist = [&nbsp; &nbsp; &nbsp; 'http://localhost:8000'&nbsp; &nbsp; ];&nbsp; &nbsp; // All the headers must be a string&nbsp; &nbsp; private static $allowedOrigin = '*';&nbsp; &nbsp; private static $allowedMethods = 'OPTIONS, GET, POST, PUT, PATCH, DELETE';&nbsp; &nbsp; private static $allowCredentials = 'true';&nbsp; &nbsp; private static $allowedHeaders = '';&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* Handle an incoming request.&nbsp; &nbsp; &nbsp;*&nbsp; &nbsp; &nbsp;* @param&nbsp; \Illuminate\Http\Request&nbsp; $request&nbsp; &nbsp; &nbsp;* @param&nbsp; \Closure&nbsp; $next&nbsp; &nbsp; &nbsp;* @return mixed&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; public function handle($request, Closure $next)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; if (! $this->isCorsRequest($request))&nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; return $next($request);&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; static::$allowedOrigin = $this->resolveAllowedOrigin($request);&nbsp; &nbsp; &nbsp; static::$allowedHeaders = $this->resolveAllowedHeaders($request);&nbsp; &nbsp; &nbsp; $headers = [&nbsp; &nbsp; &nbsp; &nbsp; 'Access-Control-Allow-Origin'&nbsp; &nbsp; &nbsp; &nbsp;=> static::$allowedOrigin,&nbsp; &nbsp; &nbsp; &nbsp; 'Access-Control-Allow-Methods'&nbsp; &nbsp; &nbsp; => static::$allowedMethods,&nbsp; &nbsp; &nbsp; &nbsp; 'Access-Control-Allow-Headers'&nbsp; &nbsp; &nbsp; => static::$allowedHeaders,&nbsp; &nbsp; &nbsp; &nbsp; 'Access-Control-Allow-Credentials'&nbsp; => static::$allowCredentials,&nbsp; &nbsp; &nbsp; ];&nbsp; &nbsp; &nbsp; // For preflighted requests&nbsp; &nbsp; &nbsp; if ($request->getMethod() === 'OPTIONS')&nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; return response('', 200)->withHeaders($headers);&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; $response = $next($request)->withHeaders($headers);&nbsp; &nbsp; &nbsp; return $response;&nbsp; &nbsp; }&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* Incoming request is a CORS request if the Origin&nbsp; &nbsp; &nbsp;* header is set and Origin !== Host&nbsp; &nbsp; &nbsp;*&nbsp; &nbsp; &nbsp;* @param&nbsp; \Illuminate\Http\Request&nbsp; $request&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; private function isCorsRequest($request)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; $requestHasOrigin = $request->headers->has('Origin');&nbsp; &nbsp; &nbsp; if ($requestHasOrigin)&nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; $origin = $request->headers->get('Origin');&nbsp; &nbsp; &nbsp; &nbsp; $host = $request->getSchemeAndHttpHost();&nbsp; &nbsp; &nbsp; &nbsp; if ($origin !== $host)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return true;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; return false;&nbsp; &nbsp; }&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* Dynamic resolution of allowed origin since we can't&nbsp; &nbsp; &nbsp;* pass multiple domains to the header. The appropriate&nbsp; &nbsp; &nbsp;* domain is set in the Access-Control-Allow-Origin header&nbsp; &nbsp; &nbsp;* only if it is present in the whitelist.&nbsp; &nbsp; &nbsp;*&nbsp; &nbsp; &nbsp;* @param&nbsp; \Illuminate\Http\Request&nbsp; $request&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; private function resolveAllowedOrigin($request)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; $allowedOrigin = static::$allowedOrigin;&nbsp; &nbsp; &nbsp; // If origin is in our $allowedOriginsWhitelist&nbsp; &nbsp; &nbsp; // then we send that in Access-Control-Allow-Origin&nbsp; &nbsp; &nbsp; $origin = $request->headers->get('Origin');&nbsp; &nbsp; &nbsp; if (in_array($origin, static::$allowedOriginsWhitelist))&nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; $allowedOrigin = $origin;&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; return $allowedOrigin;&nbsp; &nbsp; }&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* Take the incoming client request headers&nbsp; &nbsp; &nbsp;* and return. Will be used to pass in Access-Control-Allow-Headers&nbsp; &nbsp; &nbsp;*&nbsp; &nbsp; &nbsp;* @param&nbsp; \Illuminate\Http\Request&nbsp; $request&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; private function resolveAllowedHeaders($request)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; $allowedHeaders = $request->headers->get('Access-Control-Request-Headers');&nbsp; &nbsp; &nbsp; return $allowedHeaders;&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP