laravel 具有相同路由组的多个前缀
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/45809006/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
multiple prefix with the same route group
提问by hretic
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
user selects 1 on these and website contents changes based on the user selection
用户在这些上选择 1,网站内容根据用户选择而变化
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 -------------
- - - - - - - - - - - - - 更新 - - - - - - -
i've tried this
我试过这个
$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
它工作正常,问题是最后一个前缀附加到所有内部链接
for example if i click on math
例如,如果我点击数学
my links will be
我的链接将是
site.com/math/news
site.com/math/news
but all the links on the loaded page like
但是加载页面上的所有链接都像
<a href="{{route('article_index')"> link to article </a>
look like
看起来像
site.com/geography/article
site.com/geography/article
basically link get the last mentioned prefix regardless of currently selected one
基本上链接获取最后提到的前缀,无论当前选择的是什么
采纳答案by louisfischer
Why not do it this way:
为什么不这样做:
$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.
我知道这是一个基本的方法。然而,您可以轻松添加主题,清晰易懂。
Update
更新
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");
});
}
回答by Wreigh
I think it's better to do:
我认为最好这样做:
Route::get('/news/{group}', 'NewsController@index')->name('news_index')->where('group', 'math|geography|chemistry');
And then just put condition on the controller function whether it is geography/math/chemistry/etc.
然后只需在控制器功能上设置条件,无论是地理/数学/化学/等。
Don't you think?
你不觉得吗?
回答by Govind Samrow
You can try following:
您可以尝试以下操作:
$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);
Use as following:
使用方法如下:
{!!URL::to('chemistry/news')!!}
{!!URL::to('geography/news')!!}
{!!URL::to('math/news')!!}
回答by Desh901
You could try to use the as
option within your groups to tell the Router to prepend a string to every route name within that group. To do so try the following:
您可以尝试as
在您的组中使用该选项来告诉路由器在该组中的每个路由名称前添加一个字符串。为此,请尝试以下操作:
Route::group(['prefix' => 'chemistry', 'as' => 'chemistry.'], $myroutes);
Route::group(['prefix' => 'math', 'as' => 'math.'], $myroutes);
Route::group(['prefix' => 'geography', 'as' => 'geography.'], $myroutes);
So what you will be able to do should be:
所以你能做的应该是:
<a href="{{route('chemistry.article_index')}}"> link to article </a>
<a href="{{route('math.article_index')}}"> link to article </a>
<a href="{{route('geography.article_index'}})"> link to article </a>
Hope it helps.
希望能帮助到你。
回答by Norris Oduro
You can wildcard the route group and specify the preferred prefixes in your RouteServiceProvider
您可以通配路由组并指定首选前缀 RouteServiceProvider
routes.php
路由文件
Route::group(['prefix'=>'{slug}'],function (){
Route::get('/news', 'NewsController@index')->name('news_index');
Route::get('/article', 'ArticleController@index')->name('article_index');
});
RouteServiceProvider boot method
RouteServiceProvider 启动方法
Route::bind('slug',function ($name){
$prefix = ["math","chemistry","geography"];
if(!in_array($name,$prefix))
{
//handle wrong prefixes
throw new \Exception("Something went wrong");
}
});
use named route
使用命名路由
{{route('news_index',['slug'=>'math'])}}
{{route('news_index',['slug'=>'math'])}}
回答by Don't Panic
There are several good answers here already, it is probably just a matter of personal preference or deeper project specifics which one suits. Here's another option for the pile.
这里已经有几个很好的答案,这可能只是个人喜好或更深层次的项目细节的问题。这是桩的另一种选择。
I am not sure why @Shams answer was downvoted, it seems like the cleanest approach to me - but only if the prefixes are constrained so that only valid subjects are accepted. Something like:
我不确定为什么@Shams 的回答被否决了,这对我来说似乎是最干净的方法 - 但前提是前缀受到限制,以便只接受有效的主题。就像是:
// Only 1 place to update if you add subjects
$subjectRegex = 'math|geography|chemistry';
// Only 1 route per 'group'
Route::get('{subject}/news', 'NewsController@index')->name('news_index')->where('subject', $subjectRegex);
Route::get('{subject}/article', 'ArticleController@index')->name('article_index')->where('subject', $subjectRegex);
As a bonus you have $subject
available in your Controller methods, which seems like it might be useful, for example you can use it to generate routes within the current subject:
作为奖励,您$subject
可以在 Controller 方法中使用它,这似乎很有用,例如您可以使用它在当前主题中生成路由:
route('article_index', ['subject' => $subject])
回答by Oluwatobi Samuel Omisakin
Just for Curiosity sake I attempted optional parameter on prefix route grouping in laravel and it worked. Check it out:
出于好奇,我尝试了在 laravel 中前缀路由分组的可选参数,并且它起作用了。一探究竟:
Route::group(['prefix' => '{subject?}', 'as'=> 'subject.', where' => ['subject' => 'math|english|geo']],function (){
Route::get('news', function (){
return 'This is the news';
})->name('news');
});
Pretty sure this is the solution you dreamt of.
很确定这是您梦寐以求的解决方案。
Well before this would be the correct answer, there might be a little issue. Calling route('subject.news')
will give http://example.com/news
. To make it happy, you have to pass the optional parameter to route()
function i.e. route('subject.news','math');
for example; then you'll have http://example.com/math/news
.
在这成为正确答案之前,可能会有一个小问题。打电话route('subject.news')
会给http://example.com/news
。为了让它开心,您必须将可选参数传递给route()
函数 ie route('subject.news','math');
,例如;那么你会有http://example.com/math/news
.
PS: This was done on Laravel 5.4.30 PHP 7.1
PS:这是在 Laravel 5.4.30 PHP 7.1 上完成的
回答by Shams Reza
Instead of grouping you can use route parameters
您可以使用路由参数代替分组
Route::get('/{prefix}/news', 'NewsController@index')->name('news_index');
Route::get('/{prefix}/article', 'ArticleController@index')->name('article_index');