route.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. <?php
  2. use think\facade\Route;
  3. /*
  4. * 路由未匹配到任何路由时的处理函数
  5. */
  6. Route::miss(function () {
  7. // 获取当前请求的路径
  8. $appRequest = request()->pathinfo();
  9. // 如果路径为空,则设置应用名称为空字符串
  10. if ($appRequest === null) {
  11. $appName = '';
  12. } else {
  13. // 将双斜杠替换为单斜杠
  14. $appRequest = str_replace('//', '/', $appRequest);
  15. // 获取应用名称
  16. $appName = explode('/', $appRequest)[0] ?? '';
  17. }
  18. switch (strtolower($appName)) {
  19. // 如果应用名称为 admin、kefu 或 app,则返回对应的视图
  20. case config('app.admin_prefix', 'admin'):
  21. case 'kefu':
  22. case 'app':
  23. return view(app()->getRootPath() . 'public' . DS . config('app.admin_prefix', 'admin') . DS . 'index.html');
  24. // 如果应用名称为 home,则根据请求类型返回不同的视图或重定向
  25. case 'home':
  26. // 如果是移动端访问
  27. if (request()->isMobile()) {
  28. return redirect(app()->route->buildUrl('/'));
  29. } else {
  30. return view(app()->getRootPath() . 'public' . DS . 'home' . DS . 'index.html');
  31. }
  32. // 如果应用名称为 pages,则返回 index.html 视图
  33. case 'pages':
  34. return view(app()->getRootPath() . 'public' . DS . 'index.html');
  35. default:
  36. // 如果不是移动端访问
  37. if (!request()->isMobile()) {
  38. // 如果请求类型为 PC,并且 home 目录存在且没有 mdType 参数,则返回 home/index.html 视图
  39. if (is_dir(app()->getRootPath() . 'public' . DS . 'home') && !request()->get('mdType')) {
  40. return view(app()->getRootPath() . 'public' . DS . 'home' . DS . 'index.html');
  41. } else {
  42. if (request()->get('type')) {
  43. return view(app()->getRootPath() . 'public' . DS . 'index.html');
  44. } else {
  45. return view(app()->getRootPath() . 'public' . DS . 'mobile.html', ['siteName' => sys_config('site_name'), 'siteUrl' => sys_config('site_url') . '/pages/index/index']);
  46. }
  47. }
  48. } else {
  49. return view(app()->getRootPath() . 'public' . DS . 'index.html');
  50. }
  51. }
  52. });