CommonInit.php 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // |
  4. // +----------------------------------------------------------------------
  5. // |
  6. // +----------------------------------------------------------------------
  7. // |
  8. // +----------------------------------------------------------------------
  9. // |
  10. // +----------------------------------------------------------------------
  11. // +----------------------------------------------------------------------
  12. // | 初始化中间件配置
  13. // +----------------------------------------------------------------------
  14. declare (strict_types = 1);
  15. namespace app\common\middleware;
  16. use Closure;
  17. use think\facade\Config;
  18. use think\facade\Env;
  19. use think\Request;
  20. use think\Response;
  21. class CommonInit
  22. {
  23. /**
  24. * 框架初始化.
  25. *
  26. * @param Request $request
  27. * @param Closure $next
  28. *
  29. * @return Response
  30. */
  31. public function handle($request, Closure $next)
  32. {
  33. // 设置mbstring字符编码
  34. mb_internal_encoding('UTF-8');
  35. //设置DEBUG环境
  36. $this->initDebugEnv();
  37. // Form别名
  38. if (!class_exists('Form')) {
  39. class_alias('form\\Form', 'Form');
  40. }
  41. return $next($request);
  42. }
  43. /**
  44. * 调试模式缓存
  45. */
  46. private function initDebugEnv()
  47. {
  48. if (Env::get('APP_DEBUG')) {
  49. // 如果是调试模式将version置为当前的时间戳可避免缓存
  50. Config::set(['version' => time()], 'site');
  51. }
  52. }
  53. }