Laravel 5.5 url 中的语言环境作为前缀
Laravel 5.5 locale as prefix in url
目前我的 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' );
} );
这并没有真正起作用。
我想要的是 url 这样的:
/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
所以我希望 locale 参数在 url 的开头是可选的。
到目前为止,我设法实现的只是让 url 在我自己指定语言环境时工作。当我不手动指定语言环境时,我总是收到 not found
消息。
我知道我应该可以这样做:
Route::get('/path/{id}/{start?}/{end?}', ['as' => 'route.name', 'uses' => 'PathController@index']);
public function index($id, $start = "2015-04-01", $end = "2015-04-30")
{
// code here
}
但我认为这是一个但意味着我必须在每个控制器中设置默认语言环境,在我看来这有点难看。另外,我认为这应该可以在 Laravel.
中以更优雅的方式实现
如何为 url 中的 locale
前缀设置默认值?
你必须深入思考生成的路由。并且 永远不要使用前缀作为可选 ,所以要使所有工作 url 像这样改变路线
Route::get( '/', 'LandingController@index' )->name( 'home' );
Route::get( '/hero/create', 'HeroController@create' )->name( 'hero.create' );
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' );
} );
这是您的方式的简短描述
/ # should work with first above route
/create/hero # should work with first above route
/fr/create/hero # should work with route inside prefix
/nl/create/hero # should work with route inside prefix
/fr # should work with route inside prefix
/nl # should work with route inside prefix
要么将可选的({locale?})放在最后而不是中间,或者你可以将路由放入变量并同时放入两个条件,我的意思是外部前缀和内部前缀。
$heroRoutes = function (\Illuminate\Routing\Router $router) {
Route::get( '/', 'LandingController@index' )->name( 'home' );
Route::get( '/hero/create', 'HeroController@create' )->name( 'hero.create' );
}
Route::group( [ 'middleware' =>\App\Http\Middleware\Locale::class ], $heroRoutes );
Route::group( [ 'prefix' => '{locale}', 'middleware' =>\App\Http\Middleware\Locale::class ], $heroRoutes );
目前我的 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' );
} );
这并没有真正起作用。
我想要的是 url 这样的:
/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
所以我希望 locale 参数在 url 的开头是可选的。
到目前为止,我设法实现的只是让 url 在我自己指定语言环境时工作。当我不手动指定语言环境时,我总是收到 not found
消息。
我知道我应该可以这样做:
Route::get('/path/{id}/{start?}/{end?}', ['as' => 'route.name', 'uses' => 'PathController@index']);
public function index($id, $start = "2015-04-01", $end = "2015-04-30")
{
// code here
}
但我认为这是一个但意味着我必须在每个控制器中设置默认语言环境,在我看来这有点难看。另外,我认为这应该可以在 Laravel.
中以更优雅的方式实现如何为 url 中的 locale
前缀设置默认值?
你必须深入思考生成的路由。并且 永远不要使用前缀作为可选 ,所以要使所有工作 url 像这样改变路线
Route::get( '/', 'LandingController@index' )->name( 'home' );
Route::get( '/hero/create', 'HeroController@create' )->name( 'hero.create' );
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' );
} );
这是您的方式的简短描述
/ # should work with first above route
/create/hero # should work with first above route
/fr/create/hero # should work with route inside prefix
/nl/create/hero # should work with route inside prefix
/fr # should work with route inside prefix
/nl # should work with route inside prefix
要么将可选的({locale?})放在最后而不是中间,或者你可以将路由放入变量并同时放入两个条件,我的意思是外部前缀和内部前缀。
$heroRoutes = function (\Illuminate\Routing\Router $router) {
Route::get( '/', 'LandingController@index' )->name( 'home' );
Route::get( '/hero/create', 'HeroController@create' )->name( 'hero.create' );
}
Route::group( [ 'middleware' =>\App\Http\Middleware\Locale::class ], $heroRoutes );
Route::group( [ 'prefix' => '{locale}', 'middleware' =>\App\Http\Middleware\Locale::class ], $heroRoutes );