具有相同路由组的多个前缀

编程入门 行业动态 更新时间:2024-10-12 08:27:19
本文介绍了具有相同路由组的多个前缀的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在为学校编写一个相当简单的网站...该网站上有新闻,文章,视频片段等

im writing a fairly simple website for a school ... this website has news , articles , video clips ... etc

它的工作方式是在主页上,向访问者展示诸如

the way it works is in the home page we present visitor with some lessons like

>math >geography >chemistry

用户在其中选择1,然后根据用户选择更改网站内容

user selects 1 on these and website contents changes based on the user selection

例如,如果用户选择数学,他将看到新闻,文章,有关数学的视频等……现在,这就是即时通讯正在做的事情(请忽略语法错误)

for example if user selects math he will see news , article , videos about math and so on ... right now this is what im doing (pleas ignore syntax errors)

Route::group(['prefix'=>'math'], function () { Route::get('/news', 'NewsController@index')->name('news_index'); Route::get('/article', 'ArticleController@index')->name('article_index'); }); Route::group(['prefix'=>'geography'], function () { Route::get('/news', 'NewsController@index')->name('news_index'); Route::get('/article', 'ArticleController@index')->name('article_index'); }); Route::group(['prefix'=>'chemistry'], function () { Route::get('/news', 'NewsController@index')->name('news_index'); Route::get('/article', 'ArticleController@index')->name('article_index'); });

基本上为每个前缀重复所有链接....但是随着链接的增长,它将变得越来越难以管理...还有更好的方法吗?像

basically repeating all links for each prefix .... but as the links grow it will become more and more unmanageable ... is there any better way to do this ? something like

Route::group(['prefix'=>['chemistry','math' , 'geography' ], function () { Route::get('/news', 'NewsController@index')->name('news_index'); Route::get('/article', 'ArticleController@index')->name('article_index'); });

-------------------------更新-------------

------------------------- update -------------

我已经尝试过

$myroutes = function () { Route::get('/news', 'NewsController@index')->name('news_index'); Route::get('/article', 'ArticleController@index')->name('article_index'); }; Route::group(['prefix' => 'chemistry'], $myroutes); Route::group(['prefix' => 'math'], $myroutes); Route::group(['prefix' => 'geography'], $myroutes);

,它工作正常,问题是最后一个前缀附加到所有内部链接上

and it works fine , the problem is the last prefix gets attached to all the internal links

例如,如果我单击数学

我的链接将会

site/math/news

site/math/news

但已加载页面上的所有链接都像

but all the links on the loaded page like

<a href="{{route('article_index')"> link to article </a>

看起来像

site/geography/article

site/geography/article

基本上链接会获得最后提到的前缀,而不管当前选择的前缀是什么

basically link get the last mentioned prefix regardless of currently selected one

推荐答案

为什么不这样:

$subjects = [ 'chemistry', 'geography', 'math' ]; foreach ($subjects as $subject) { Route::prefix($subject)->group(function () { Route::get('news', 'NewsController@index')->name('news_index'); Route::get('article', 'ArticleController@index')->name('article_index'); }); }

我知道这是基本的方法.然而,您可以轻松添加主题,这很容易理解.

I know this is an elementary way do to it. Yet you can easily add subjects, it is clear and effortless to understand.

更新

正如评论中指出的那样,按主题命名路线可能很方便,这是执行此操作的方法:

As pointed in the comments it could be convenient to name the route as per subject, here is how to do this:

$subjects = [ 'chemistry', 'geography', 'math' ]; foreach ($subjects as $subject) { Route::prefix($subject)->group(function () use ($subject) { Route::get('news', 'NewsController@index')->name("{$subject}_news_index"); Route::get('article', 'ArticleController@index')->name("{$subject}_article_index"); }); }

更多推荐

具有相同路由组的多个前缀

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

发布评论

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

>www.elefans.com

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