lumen中间件中设置响应header

<?php

namespace App\Http\Middleware;

use Closure;

class BeforeMiddleware
{

    public function handle($request, Closure $next)
    {
        // 执行动作

        $origin = isset($_SERVER['HTTP_ORIGIN']) ? $_SERVER['HTTP_ORIGIN'] : '';

        $allow_origin = array(
            'http://m.c.com',
            'http://www.t.com'
        );

        if(in_array($origin, $allow_origin)){
            header('Access-Control-Allow-Origin:'.$origin);
            header('Access-Control-Allow-Headers:Content-Type, Accept, Authorization, X-Requested-With');
            header('Access-Control-Allow-Methods:POST, GET, OPTIONS, PUT, DELETE, PATCH');
        }

        return $next($request);
    }

}

然后bootstrap/app.php中注册该中间件:

$app->middleware([
    Illuminate\Session\Middleware\StartSession::class,
    App\Http\Middleware\VerifyCsrfToken::class,
    App\Http\Middleware\EncryptCookies::class,
    App\Http\Middleware\BeforeMiddleware::class
]);

 

posted on 2017-06-22 09:47  dongruiha  阅读(2119)  评论(0编辑  收藏  举报

导航