Php Laravel 5.5语言环境作为url中的前缀

Php Laravel 5.5语言环境作为url中的前缀,php,laravel,laravel-5,laravel-5.5,Php,Laravel,Laravel 5,Laravel 5.5,目前我的routes/web.php中有以下内容: Route::group( [ 'prefix' => '{locale?}', 'middleware' =>\App\Http\Middleware\Locale::class ], function (\Illuminate\Routing\Router $router) { Route::get( '/', 'LandingController@index' )->name( 'home' ); Ro

目前我的
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
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
}
因此,我希望url开头的locale参数是可选的。 到目前为止,我所实现的只是在自己指定语言环境时让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
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
}
但我认为这是一个错误,但这意味着我必须在每个控制器中设置默认区域设置,这在我看来有点难看。此外,我认为这应该是一个更优雅的方式在拉雷维尔可能


如何为url中的
区域设置
前缀设置默认值?

您必须深入思考生成的路由。而且永远不要使用前缀作为可选的,所以要使所有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?})放在最后一个位置而不是中间位置,也可以将route放在一个变量中并同时放在两个条件中,我指的是外部前缀和内部前缀

$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 );

我知道我可以这样做。但是当有人决定共享一个像/hero/create这样的url时,我不认为它会导致404。我希望该路线也可用,并使用默认语言。因此,我希望语言参数是可选的。@SheperdOfFire:如果您像您所说的那样使用
/hero/create
,第一个单词
hero
将是
locale
的值,而不是您预期的值,您将得到
404
错误。我希望你明白我的意思?是的,我明白,但这也意味着我必须创建每条路线两次。我希望会有一个自动化的解决方案,它可以解决要么把可选(
{locale?}
)放在最后,而不是中间,要么把route放在一个变量中,然后把这两个条件都放进去,我指的是外部前缀和内部前缀。你可以从其他人那里得到一些想法,你是否考虑过将
/
/create/hero
重定向到默认的语言环境版本,而不是在这些URL上提供真实的内容?