【问题标题】:Dynamic Pages routes in LaravelLaravel 中的动态页面路由
【发布时间】:2016-03-05 15:37:55
【问题描述】:

这个问题看起来很简单,但我想不通……你如何在 Laravel 5.1 中实现带有类别的基于 slug 的动态页面?

类似mywebsite.com/category/sub-category/my-page

我知道Route::get('/{slug}') 将“slug”作为标识符,但问题是该页面可能位于mywebsite.com/category/my-page,因此如果将路由设置为Route::get('/{category}/{subcategory}/{page}') 之类的内容,则第二个将不起作用例子。

我正在考虑制作 3 条路线,例如

Route::get('/{category}/{subcategory}/{page}')
Route::get('/{category}/{page}')
Route::get('/{page}')

然后控制器接收($category = null, $subcategory = null, $page = null) 在控制器中类似于

if (!$page)
    $page = $subcategory;

if (!$page)
    $page = $category;

是否有另一种更清洁的方法来实现这一目标?因为这里我们只有 2 个类别路线,但可能有 3 个、5 个或更多?

【问题讨论】:

  • 这似乎是设计不佳的产物。如果您不需要类别和子类别来显示页面,请不要强迫您的用户浏览它。如果您不知何故被这些路由卡住,请使用request()->page 获取页面,而不是摸索函数参数。
  • 我想知道您是否可以将Route::controllerpublic function getSomePage(Request $request) 之类的动态 方法一起使用,然后在$request->segments() 上执行手动方法 - 不过没有尝试过,阅读the doc 可能会提供一些见解。 ps. route::controller 虽然有一个预定义的参数,即。 getSomePage($category, $slug)route::list 中仍显示为SomePage/{one?}/{two?}/.../{five?}
  • @Ivan Milanovm ,如果使用Route::get('/{page}') 如何显示其他网址?例如/user 或...不是page 的网址。

标签: php laravel laravel-5.1


【解决方案1】:

好的,所以基本上你希望有一个类别的类别,并且能够通过这些类别访问帖子。我假设你有这样的模型:

class Category extends Model
{
  public function SubCategories()
  {
    return $this->hasMany('Category', 'parent_id', 'id');
  }
  public function ParentCategory()
  {
    return $this->belongsTo('Category', 'parent_id', 'id);
  }
  public function Page()
  {
    return $this->hasMany('Page', 'category_id', 'id');
  }
}

然后创建一个控制器来检索帖子

class PageLoaderController extends Controller
{
   //assumes you want to run this via root url (/)
   public function getIndex(Requests $request)
   {
     //this practically get you any parameter after public/ (root url)
     $categories = $request->segments();
     $countSeg   = count($categories);
     $fcategory  = null;
     $fpage      = null;
     for($i = 0; $i < $countSeg; $i++)
     {
       //this part of iteration is hypothetical - you had to try it to make sure it had the desired outcome
       if(($i + 1) == $countSeg)
       {
         //make sure fcategory != null, it should throw 404 otherwise
         //you had to add code for that here
         $fpage = $fcategory->Page()->where('slug', $categories[$i])->firstOrFail();
       }
       elseif($i == 0)
       {
         //find initial category, if no result, throw 404
         $fcategory = Category::where('slug', $categories[0])->firstOrFail();
       }
       else
       {
         //take it's child category if possible, otherwise throw 404
         $fcategory = $fcategory->SubCategories()->where('slug', $categories[$i])->firstOrFail()
       }
     }
     //do something with your $fpage, render it or something else
   }
}

然后添加到您的route.php:

Route::controller('/','PageLoaderController', [
                  'getIndex' => 'home'
                  ]);

应该可以。但是,这是一种 dirty 方法,我不建议广泛使用它。一些原因是,http get length 不受限制,但过长是一种不好的做法,这种方法不适合 CRUD - 改用Route::resource,循环看起来也很痛苦。

注意Route::controllerRoute::resource 更狂野,您必须按照 Route::&lt;http verb&gt;Route::resourceRoute::controller 的顺序将条目放入 route.php。到目前为止,我遇到了 Laravel 路由行为,即使它没有关于请求的 url 的方法并抛出 NotFoundHttpException,它也会戳Route::controller。所以如果你在Route::controller 之后有Route::&lt;http verb&gt;,laravel 往往根本不会碰它。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-11-28
    • 2020-04-06
    • 1970-01-01
    • 2022-11-20
    • 2020-09-01
    • 2017-07-19
    • 2022-08-15
    • 1970-01-01
    相关资源
    最近更新 更多