问题描述
例如,我正在出版包含章节,主题和文章的图书:
For example, I'm publishing books with chapters, topics, articles:
http://domain.com/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) {
...
}
有关更多信息,请参阅文档: http://laravel.com/docs/5.0 /routing#route-parameters
See the docs for more info: http://laravel.com/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路由?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!