CORS 跨站访问
使用 CORS 中间定义跨站的请求策略,你需要在主配置或对应的模块下创建配置 config/cors.php
,例如:
return [
'allow_origin' => [
'https://api.developer.com'
],
'with_credentials' => true,
'option_max_age' => 2592000,
'methods' => 'GET,OPTIONS,POST,PUT',
'headers' => 'Content-Type,X-Requested-With,X-Token'
];
注册中间件,修改主配置目录下 config/middleware.php
return [
'cors' => \bit\middleware\Cors::class,
];
在控制器中使用
namespace app\index\controller;
use think\Controller;
class Index extends Controller
{
protected $middleware = ['cors'];
public function index()
{
return 'index';
}
}
在路由中使用
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。
SystemLog 系统日志收集
使用 SystemLog 系统日志, 首先需要配置 Rabbitmq
,并安装库
composer require kain/think-logging
然后修改配置 config/queue.php
return [
'logging' => [
'exchange' => 'system',
'queue' => 'system'
]
];
- exchange 交换器
- queue 队列
注册中间件,修改主配置目录下 config/middleware.php
return [
'systemLog' => think\logging\middleware\SystemLog::class
];
在控制器中使用
namespace app\system\controller;
use think\Controller;
class Base extends Controller
{
protected $middleware = ['systemLog'];
}
使用前对应配置队列写入服务 https://github.com/kainonly/amqp-logging-service