Browse Source

路由修改

moshaorui 3 weeks ago
parent
commit
eece070153
3 changed files with 51 additions and 28 deletions
  1. 1 0
      app/Http/Kernel.php
  2. 22 0
      app/Http/Middleware/MapCustomUrl.php
  3. 28 28
      routes/web.php

+ 1 - 0
app/Http/Kernel.php

@@ -21,6 +21,7 @@ class Kernel extends HttpKernel
         \Illuminate\Foundation\Http\Middleware\ValidatePostSize::class,
         \App\Http\Middleware\TrimStrings::class,
         \Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class,
+        \App\Http\Middleware\MapCustomUrl::class,   //自定义路由映射
         \App\Http\Middleware\LoadDistData::class,   //读取分销商信息
     ];
 

+ 22 - 0
app/Http/Middleware/MapCustomUrl.php

@@ -0,0 +1,22 @@
+<?php
+
+namespace App\Http\Middleware;
+
+use Closure;
+use Illuminate\Http\Request;
+
+class MapCustomUrl
+{
+    public function handle(Request $request, Closure $next)
+    {
+        $path = $request->path();
+
+        // 检查是否是 /protetor-de-tela-de-vidro-temperado
+        if ($path === 'sobre') {
+            // 动态设置路由参数
+            $request->route()->setParameter('slug', 'sobre');
+        }
+
+        return $next($request);
+    }
+}

+ 28 - 28
routes/web.php

@@ -24,34 +24,34 @@ use App\Models\SitePage;
 */
 
 //巴西官网专用路由
-$host = $_SERVER['HTTP_HOST']; // 获取当前域名
-
-if ($host == 'internal-demo-site.mietubl.com.cn') {
-    Route::get('/{slug}', function ($slug) {
-        // 查询数据库,将 category_slug 转换为对应ID
-        $categoryId = DistProductCategory::where(function ($query) use ($slug) {
-            $query->where('slug', $slug);
-        })
-            ->where('dist_id', getDistId())
-            ->first();
-
-        if ($categoryId) {
-            $productController = new ProductController();
-            return $productController->category($slug);
-        } else {
-            $page = SitePage::where('status', '1')->where('dist_id', getDistId())->where('slug', $slug)->first();
-            if ($page) {
-//                $pageController = new PageController();
-//                return $pageController->detail($page);
-                return app()->call([PageController::class, 'detail'], ['slug' => $slug]);
-            }
-        }
-    });
-    Route::get('/produto/{slug}', function ($slug) {
-        $productController = new ProductController();
-        return $productController->detail($slug);
-    });
-}
+//$host = $_SERVER['HTTP_HOST']; // 获取当前域名
+//
+//if ($host == 'internal-demo-site.mietubl.com.cn') {
+//    Route::get('/{slug}', function ($slug) {
+//        // 查询数据库,将 category_slug 转换为对应ID
+//        $categoryId = DistProductCategory::where(function ($query) use ($slug) {
+//            $query->where('slug', $slug);
+//        })
+//            ->where('dist_id', getDistId())
+//            ->first();
+//
+//        if ($categoryId) {
+//            $productController = new ProductController();
+//            return $productController->category($slug);
+//        } else {
+//            $page = SitePage::where('status', '1')->where('dist_id', getDistId())->where('slug', $slug)->first();
+//            if ($page) {
+////                $pageController = new PageController();
+////                return $pageController->detail($page);
+//                return Route::get('/{slug}', [PageController::class, 'detail'])->name('page.detail');
+//            }
+//        }
+//    });
+//    Route::get('/produto/{slug}', function ($slug) {
+//        $productController = new ProductController();
+//        return $productController->detail($slug);
+//    });
+//}