首页 > 解决方案 > 更改路线方向取决于所选语言

问题描述

所以这是我的路线

Route::group(['prefix' => '{locale}'],function(){
Route::get('/','Room_randring_welcom@function0')->name('post')->middleware('setLocale');});

例如,如果我在 lang 或任何其他我想去的语言中选择 es

Route::group(['prefix' => '{locale}'],function(){
Route::get('/','Room_randring_welcom@function1')->name('post')->middleware('setLocale');});

**所以我可以使用另一种语言从另一个 JSON 文件中获取数据**

标签: phplaravel

解决方案


您可以通过编辑来完成RouteServiceProvider,您将在以下位置找到它root-app/app/Providers/RouteServiceProvider.php

对于 Laravel <= 7

public function map()
{

   $this->mapApiRoutes();

   $this->mapWebRoutes();

   $this->mapArRoutes(); // Add this line with local name or anything you want.
}

//add function with the new name in my case will be "mapArRoutes"

protected function mapArRoutes()
{

    Route::middleware('web')
       ->prefix('ar') // Here choose a local do you want
       ->namespace($this->namespace)
       ->group(base_path('routes/ar.php')); // any name do you want
}

// You will find a function called "mapWebRoutes", edit it.

protected function mapWebRoutes()
{
    // This is a default route you just need add perfix with anothoer local do you want
   Route::middleware('web')
      ->prefix('en') // for example i added en
      ->namespace($this->namespace)
      ->group(base_path('routes/web.php'));
}

然后在路由文件夹/目录中添加新文件,作为我的示例,它将是ar.php

将所有路由添加到与本地相关的文件中。

对于 Laravel >= 8

...
public function boot()
{
 $this->configureRateLimiting();

 $this->routes(function () {
    Route::prefix('api')
       ->middleware('api')
       ->namespace($this->namespace)
       ->group(base_path('routes/api.php'));


    // Add this lines
    // Start
    Route::middleware('web')
       ->prefix('ar') // Here choose a local do you want
       ->namespace($this->namespace)
       ->group(base_path('routes/ar.php')); // any name do you want

    // This is a default route you just need add perfix with anothoer local do you want
    Route::middleware('web')
      ->prefix('en') // for example i added en
      ->namespace($this->namespace)
      ->group(base_path('routes/web.php'));
    // End


 });
}
...

希望这很清楚。

就这样。


推荐阅读