laravel 解决跨越问题

首页 / php框架 / 正文

laravel 解决跨越问题

1. 创建中间件

php artisan make:middlware AccessControlAllowOrigin

2. 中间件内容

public function handle(Request $request, Closure $next)
    {
        header('Access-Control-Allow-Origin: *');
        header("Access-Control-Allow-Credentials: true");
        header("Access-Control-Allow-Methods: *");
        header("Access-Control-Allow-Headers: Content-Type,Access-Token");
        header("Access-Control-Expose-Headers: *");
        return $next($request);
    }

3. 注册中间件

# 文件位置: app\Http\Kernel.php
protected $routeMiddleware = [
    ...
    'cors' => AccessControlAllowOrigin::class
    ...
];

4. 使用中间件

$api = app('Dingo\Api\Routing\Router');
$api->version('v1', ['middleware' => ['cors']], function ($api) {
    
});
打赏
评论区
头像
文章目录