<?php
namespace AppHttpMiddleware;
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([
IlluminateSessionMiddlewareStartSession::class,
AppHttpMiddlewareVerifyCsrfToken::class,
AppHttpMiddlewareEncryptCookies::class,
AppHttpMiddlewareBeforeMiddleware::class
]);