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

编程入门 行业动态 更新时间:2024-10-22 16:43:43
本文介绍了如何对URL中未知数量的参数使用laravel路由?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

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路由?

本文发布于:2023-10-27 11:28:18,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1533222.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:路由   数量   参数   URL   laravel

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!