我正在为一所学校编写一个相当简单的网站...该网站有新闻、文章、视频剪辑...等
它的工作方式是在主页中我们向访问者展示一些课程,例如
>math
>geography
>chemistry
用户在其中选择 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');
});
基本上重复每个前缀的所有链接....但随着链接的增长,它将变得越来越难以管理...有没有更好的方法来做到这一点?就像是
Route::group(['prefix'=>['chemistry','math' , 'geography' ], function () {
Route::get('/news', 'NewsController@index')->name('news_index');
Route::get('/article', 'ArticleController@index')->name('article_index');
});
- - - - - - - - - - - - - 更新 - - - - - - -
我试过这个
$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);
它工作正常,问题是最后一个前缀附加到所有内部链接
例如,如果我点击数学
我的链接将是
site.com/math/news
但加载页面上的所有链接都像
<a href="{{route('article_index')"> link to article </a>
看起来像
site.com/geography/article
基本上链接获取最后提到的前缀,无论当前选择的是什么