## 1. 生成令牌 你可以在`app\common.php` 中添加生成令牌的方法: ~~~ /** * 生成令牌 */ function shopToken() { $data = request()->buildToken('__token__', 'sha1'); return '<input type="hidden" name="__token__" value="' . $data . '" class="token">'; } ~~~ ## 2. 在模板表单中使用: ~~~ <div class="layui-form-item"> {:shopToken()} </div> ~~~ ## 3. 控制器验证 ~~~ namespace app\controller; use think\exception\ValidateException; use app\BaseController; class Index extends BaseController { public function index(Request $request) { $check = $this->request->checkToken('__token__', $this->request->param()); if (false === $check) { throw new ValidateException('invalid token'); } // ... } } ~~~