美文网首页
laravel开启跨域

laravel开启跨域

作者: IT宝哥哥 | 来源:发表于2019-11-19 13:51 被阅读0次
    1. 创建中间件


      image.png
    <?php namespace App\Http\Middleware;
    
    use Closure;
    use Response;
    class EnableCrossRequestMiddleware {
    
        /**WE
         * Handle an incoming request.
         *
         * @param  \Illuminate\Http\Request  $request
         * @param  \Closure  $next
         * @return mixed
         */
        public function handle($request, Closure $next)
        {
            $response = $next($request);
            $response->header('Access-Control-Allow-Origin', '*');
            $response->header('Access-Control-Allow-Headers', 'Origin, Content-Type, Cookie, Accept');
            $response->header('Access-Control-Allow-Methods', 'GET, POST, PATCH, PUT, OPTIONS');
            $response->header('Access-Control-Allow-Credentials', 'true');
            return $response;
        }
    }
    
    1. 注册中间件Kernel.php
    protected $routeMiddleware = [
            'auth' => \Illuminate\Auth\Middleware\Authenticate::class,
            'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
            'bindings' => \Illuminate\Routing\Middleware\SubstituteBindings::class,
            'can' => \Illuminate\Auth\Middleware\Authorize::class,
            'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
            'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
            'cross'=>EnableCrossRequestMiddleware::class,//增加的
        ];
    
    
    image.png
    1. 路由中使用中间件


      image.png

    相关文章

      网友评论

          本文标题:laravel开启跨域

          本文链接:https://www.haomeiwen.com/subject/czmwictx.html