如何在 URL 中对未知数量的参数使用 laravel 路由?

How to use laravel routing for unknown number of parameters in URL?

例如,我正在出版包含章节、主题、文章的书籍:

http://domain.com/book/chapter/topic/article

我会有 Laravel 路由参数:

Route::get('/{book}/{chapter}/{topic}/{article}', 'controller@func')

在 Laravel 中是否有可能有一个单一的规则来满足书籍结构中未知数量的级别(类似于 this question)?这将意味着那里有子文章,子子文章等..

你需要的是可选的路由参数:

//in routes.php
Route::get('/{book?}/{chapter?}/{topic?}/{article?}', 'controller@func');

//in your controller
public function func($book = null, $chapter = null, $topic = null, $article = null) {
  ...
}

有关详细信息,请参阅文档:http://laravel.com/docs/5.0/routing#route-parameters

更新:

如果你想文章后面的参数数量不限,可以这样做:

//in routes.php
Route::get('/{book?}/{chapter?}/{topic?}/{article?}/{sublevels?}', 'controller@func')->where('sublevels', '.*');

//in your controller
public function func($book = null, $chapter = null, $topic = null, $article = null, $sublevels = null) {
  //this will give you the array of sublevels
  if (!empty($sublevels) $sublevels = explode('/', $sublevels);
  ...
}