DeTechn Blog

解决thinkphp5跨域问题 cors协议解决

解决方案
cosr

tp5行为(具体参考TP5官方手册) //实际就是钩子

你需要在你的BaseController里注册钩子(TP5官方也叫做添加行为标签位)

<?php 
 
namespace app\api\behavior;
 
class Test 
{
    public function responseSend(&$params)
    {    // 响应头设置 我们就是通过设置header来跨域的 这就主要代码了 定义行为只是为了前台每次请求都能走这段代码
        header('Access-Control-Allow-Origin:*');
        header('Access-Control-Allow-Methods:*');
    header('Access-Control-Allow-Headers:*');
    header('Access-Control-Allow-Credentials:false');
    }

定义完我们就需要去绑定行为 绑定完才会去执行

建议绑定行为写在application下面tags.php文件中统一管理

<?php 
 'response_send' => [
        'app\\api\\behavior\\Test'   
    ],

当前页面是本站的「Google AMP」版。查看和发表评论请点击:完整版 »