本文主要是介绍Thinkphp5.1 中间件解决跨域问题,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
创建中间件
php think make:middleware Check
中间件内
public function handle($request, \Closure $next){header('Access-Control-Allow-Origin: *');header("Access-Control-Allow-Headers: Authorization, Sec-Fetch-Mode, DNT, X-Mx-ReqToken, Keep-Alive, User-Agent, If-Match, If-None-Match, If-Unmodified-Since, X-Requested-With, If-Modified-Since, Cache-Control, Content-Type, Accept-Language, Origin, Accept-Encoding");header('Access-Control-Allow-Methods: GET, POST, PATCH, PUT, DELETE');header('Access-Control-Max-Age: 1728000');if (strtoupper($request->method()) == "OPTIONS") {return response();}return $next($request);}
路由
Route::group([], function () {Route::get('code','index/Index/phoneCode');Route::get('login','index/Index/userLogin');})->middleware(Check);
这篇关于Thinkphp5.1 中间件解决跨域问题的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!