hre*_*tic 20 php laravel laravel-5.4
我为一所学校写了一个相当简单的网站...这个网站有新闻,文章,视频剪辑等
它的工作方式是在主页上我们向访客提供一些课程
>math
>geography
>chemistry
Run Code Online (Sandbox Code Playgroud)
用户根据用户选择在这些和网站内容上选择1
例如,如果用户选择数学,他将看到关于数学的新闻,文章,视频......现在这就是我在做什么(请求忽略语法错误)
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');
});
Run Code Online (Sandbox Code Playgroud)
基本上重复每个前缀的所有链接....但随着链接的增长,它将变得越来越难以管理......有没有更好的方法来做到这一点?就像是
Route::group(['prefix'=>['chemistry','math' , 'geography' ], function () {
Route::get('/news', 'NewsController@index')->name('news_index');
Route::get('/article', 'ArticleController@index')->name('article_index');
});
Run Code Online (Sandbox Code Playgroud)
-------------------------更新-------------
我试过这个
$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);
Run Code Online (Sandbox Code Playgroud)
它工作正常,问题是最后一个前缀附加到所有内部链接
例如,如果我点击数学
我的链接将是
site.com/math/news
但是加载页面上的所有链接都像
<a href="{{route('article_index')"> link to article </a>
Run Code Online (Sandbox Code Playgroud)
看起来像
site.com/geography/article
基本上链接获取最后提到的前缀,无论当前选择的前缀
lou*_*her 14
为什么不这样做:
$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');
});
}
Run Code Online (Sandbox Code Playgroud)
我知道这是一个基本的方法.然而,您可以轻松添加主题,理解清晰,轻松.
Wre*_*igh 12
我认为最好这样做:
Route::get('/news/{group}', 'NewsController@index')->name('news_index')->where('group', 'math|geography|chemistry');
Run Code Online (Sandbox Code Playgroud)
然后只需将条件放在控制器功能上,无论是地理/数学/化学等.
你不觉得吗?
您可以尝试以下方法:
$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);
Run Code Online (Sandbox Code Playgroud)
使用如下:
{!!URL::to('chemistry/news')!!}
{!!URL::to('geography/news')!!}
{!!URL::to('math/news')!!}
Run Code Online (Sandbox Code Playgroud)
您可以尝试使用组中的as选项告诉路由器将字符串添加到该组中的每个路由名称.为此,请尝试以下操作:
Route::group(['prefix' => 'chemistry', 'as' => 'chemistry.'], $myroutes);
Route::group(['prefix' => 'math', 'as' => 'math.'], $myroutes);
Route::group(['prefix' => 'geography', 'as' => 'geography.'], $myroutes);
Run Code Online (Sandbox Code Playgroud)
所以你能做的应该是:
<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>
Run Code Online (Sandbox Code Playgroud)
希望能帮助到你.