例如,我正在出版包含章节,主题和文章的图书:
For example, I'm publishing books with chapters, topics, articles:
domain/book/chapter/topic/article
我将使用带有参数的Laravel路线:
I would have Laravel route with parameters:
Route::get('/{book}/{chapter}/{topic}/{article}', 'controller@func')
在Laravel中是否可能有一条规则可以满足书本结构中未知数量的级别(类似于这个问题)?这意味着存在子文章,子文章等.
Is it possible, in Laravel, to have a single rule which caters for an unknown number of levels in the book structure (similar to this question)? This would mean where there are sub-articles, sub-sub-articles, etc..
推荐答案您需要的是可选的路由参数:
What you need are optional routing parameters:
//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) { ... }有关更多信息,请参阅文档: laravel/docs/5.0 /routing#route-parameters
See the docs for more info: laravel/docs/5.0/routing#route-parameters
更新:
如果您希望文章后的参数数量不受限制,则可以执行以下操作:
If you want to have unlimited number of parameters after articles, you can do the following:
//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); ... }更多推荐
如何对URL中未知数量的参数使用laravel路由?
发布评论