我为一所学校写了一个相当简单的网站...这个网站有新闻,文章,视频剪辑等
它的工作方式是在主页上我们向访客提供一些课程
>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 …Run Code Online (Sandbox Code Playgroud) 目前我有routes/web.php以下内容:
Route::group( [ 'prefix' => '{locale?}', 'middleware' =>\App\Http\Middleware\Locale::class ], function (\Illuminate\Routing\Router $router) {
Route::get( '/', 'LandingController@index' )->name( 'home' );
Route::get( '/hero/create', 'HeroController@create' )->name( 'hero.create' );
} );
Run Code Online (Sandbox Code Playgroud)
这并不像它应该的那样工作。
我想要的是有一个这样的网址:
/create/hero # should work with the default locale
/fr/create/hero # should use the french locale
/nl/create/hero # should use dutch locale
/ # should work with the default locale
/fr # should use the french locale
/nl # should use dutch locale
Run Code Online (Sandbox Code Playgroud)
所以我希望 url 开头的 locale 参数是可选的。到目前为止,我设法实现的只是在自己指定语言环境时使 url 工作。 …