RouteServiceProvider嵌套类别绑定

时间:2017-12-01 00:05:22

标签: laravel laravel-routing

我有这样的路线

  

路线:: get('/ {zoneSlug} / {categorySlug} / {sightSlug}',...

示例:

{zoneSlug}有一个表格,如伦敦或巴黎

{categorySlug}有许多形式,如:

或: cat / subcat

或: car / subcat / subcat3 /..

{sightSlug}有一个表格,如eiffel

所以我喜欢这些地址:

 example.com/paris/category/effel
 ...
 example.com/paris/category/subcategory2/.../subcategory5/effel

我尝试在app\Providers\RouteServiceProvider.php

中绑定它
.
        Route::bind('zoneSlug' , function ($value) {return Zone::whereSlug($value)->firstOrFail();});


        Route::bind('categorySlug', function ($categorySlug, $route) {dd($route);
          $zone = $route->parameter('zoneSlug');
          return Category::whereSlug($categorySlug)->firstOrFail();
        });

        Route::bind('sightSlug', function ($sightSlug, $route) {dd($route);
          $zone = $route->parameter('zoneSlug');
          $category = $route->parameter('categorySlug');
          return Sight::whereZones_id($zone->id)->whereCategories_id($category->id)->whereSlug($sightSlug)->firstOrFail();
        });

绑定代码适用于:example.com/paris/category/effel ...但不适用于

example.com/paris/category/subcategory2/.../subcategoryn/effel

我该怎么做?

1 个答案:

答案 0 :(得分:1)

我建议超越你的URI结构。您的类别更好地位于查询字符串中 example.com/paris/eiffel?categories=category/subcategory,category/subcategor2example.com/paris/eiffel?category=category/subcategory&category=category/subcategory2