Laravel, jetstream global language prefix to routes

  jetstream, laravel, routes

Is it possible to set a global language prefix to every url:
Currently php artisan route:list –compact gives

+----------+----------------------------------+-..
| Method   | URI                              | Action..
+----------+----------------------------------+-..
| GET|HEAD | api/user                         | Closur..
| GET|HEAD | forgot-password                  | LaravelFortifyHtt...
| POST     | forgot-password                  | Lara..
| GET|HEAD | livewire/livewire.js             | Livewire..
..
| GET|HEAD | login                            | Lara..
| GET|HEAD | sanctum/csrf-cookie              | Lar..
| POST     | two-factor-challenge             | L..
| POST     | user/confirm-password            | Lar..
| GET|HEAD | user/confirm-password            | La..
..
| POST     | user/two-factor-recovery-codes   | Lar..
+----------+----------------------------------+-..

but i want to (if e.g english and spain language)

+----------+----------------------------------+-..
| Method   | URI                              | Action..
+----------+----------------------------------+-..
| GET|HEAD | en/api/user                         | Closur..
| GET|HEAD | en/forgot-password                  | LaravelFortifyHtt...
| POST     | en/forgot-password                  | Lara..
| GET|HEAD | en/livewire/livewire.js             | Livewire..
..
| GET|HEAD | en/login                            | Lara..
| GET|HEAD | en/sanctum/csrf-cookie              | Lar..
| POST     | en/two-factor-challenge             | L..
| POST     | en/user/confirm-password            | Lar..
| GET|HEAD | en/user/confirm-password            | La..
..
| POST     | en/user/two-factor-recovery-codes   | Lar..

     ....the same in spain:
| GET|HEAD | es/api/user                         | Closur..
| GET|HEAD | es/forgot-password                  | LaravelFortifyHtt...


That which language is active correspondingly.
In a web.php i try to

foreach (['en', 'es'] as $k) {
    //echo (substr(url()->current(), strlen(url('/')) + 1, 2) == $k);
    Route::prefix($k)->group(function () use ($k) {
        Route::get('/', fn () => view('welcome'))->name("{$k}_home");
    });
}
Route::get('/', function () {
    return Redirect::route('es_home');
});

Thank you.

Source: Laravel

Leave a Reply