[英]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中,是否有可能只有一個規則可以滿足圖書結構中未知數量的級別(類似於這個問題 )? 這意味着哪里有子文章,子分文章等。
您需要的是可選的路由參數:
//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);
...
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.