CORS 跨站访问

使用 CORS 中间定义跨站的请求策略,你需要在主配置或对应的模块下创建配置 config/cors.php,例如:

  1. return [
  2. 'allow_origin' => [
  3. 'https://api.developer.com'
  4. ],
  5. 'with_credentials' => true,
  6. 'option_max_age' => 2592000,
  7. 'methods' => 'GET,OPTIONS,POST,PUT',
  8. 'headers' => 'Content-Type,X-Requested-With,X-Token'
  9. ];

注册中间件,修改主配置目录下 config/middleware.php

  1. return [
  2. 'cors' => \bit\middleware\Cors::class,
  3. ];

在控制器中使用

  1. namespace app\index\controller;
  2. use think\Controller;
  3. class Index extends Controller
  4. {
  5. protected $middleware = ['cors'];
  6. public function index()
  7. {
  8. return 'index';
  9. }
  10. }

在路由中使用

  1. Route::rule('index','index')->middleware('cors');

配置详情

名称 类型 说明
allow_origin array 允许跨域的域名
with_credentials boolean 允许 ajax 请求携带 Cookie
option_max_age boolean 缓存 OPTIONS 请求
methods string 允许请求类型
headers string 允许定义的头部

如果允许所有域名跨域,则将 allow_origin 设置为 [*],但 with_credentials 将失效,并且 ajax 请求不能携带 cookie。

LogSystem 系统日志

使用 LogSystem 系统日志, 首先需要 Rabbitmq 配置 config/rabbitmq.php , 然后在主配置或对应的模块下设置配置 config/log.php

  1. return [
  2. 'publish' => 'api.developer.com',
  3. 'exchange' => 'log.system',
  4. 'queue' => 'log.system'
  5. ];
  • publish 发布域名
  • exchange 交换器
  • queue 队列

注册中间件,修改主配置目录下 config/middleware.php

  1. return [
  2. 'log_system' => think\bit\middleware\LogSystem::class
  3. ];

在控制器中使用

  1. namespace app\system\controller;
  2. use think\Controller;
  3. class Base extends Controller
  4. {
  5. protected $middleware = ['log_system'];
  6. }

使用前对应配置队列写入服务 https://github.com/kainonly/collection-service