2017-08-31 128 views
0

與區域前綴在Laravel 5.4後,我想與基地前綴區域url.When我運行PHP的工匠擔任然後我得到路線NotFoundHttpException在Laravel

notfoundhttpexception

。它工作,如果我把手動http://localhost:8000/en。我現在想要什麼,當我將運行PHP的工匠服務它應該重定向到http://localhost:8000/en

以下是路線文件:

Route::group(['prefix' => App::getLocale() ], function() 
{ 
    Route::get('/', array('as' => 'home.index', 'uses' => '[email protected]')); 

}); 

回答

0

如果我正確地理解你的問題,你需要使用這樣的代碼:

Route::group(['prefix' => '{locale}' ], function() 
{ 
    Route::get('/', array('as' => 'home.index', 'uses' => '[email protected]')); 
    //Setup locale based on request... 
    App::setLocale(Request::segment(1)); 
}); 

但更好的方法可以讓我的建議是使用環境設置像:

if (in_array(Request::segment(1), ['en', 'fr'])) { 
    App::setLocale(Request::segment(1)); 
} else { 
    // set your default local if request having other then en OR fr 
    App::setLocale('en'); 
} 

並調用路徑,如:

Route::group(['prefix' => '{locale}' ], function() 
{ 
    Route::get('/', array('as' => 'home.index', 'uses' => '[email protected]')); 
}); 

通過此代碼,您可以動態地設置基於路由前綴的區域設置。