0

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                  | Laravel\Fortify\Htt...
| 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                  | Laravel\Fortify\Htt...
| 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                  | Laravel\Fortify\Htt...

... 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.

user2301515
  • 4,903
  • 6
  • 30
  • 46
  • You should be able to pull the routes out of the package and use them directly in your web.php file. See here about using ignoreRoutes(); https://stackoverflow.com/questions/64016900/laravel-localization-and-routes-from-jetstream-fortify – Cameron Sep 16 '21 at 11:47

1 Answers1

1

Route::group([ 'prefix' => '{locale}', 'where' => ['locale' => '[a-zA-Z]{2}'],'middleware' => 'setlocale']);

Faran
  • 138
  • 6