Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel localization and routes from Jetstream / Fortify

I have this new Laravel project to work on. We would like to make it available in multiple languages.

I started the project with JetStream. Routes for authentication and such are automatically handled by JetStream / Fortify. I then added https://github.com/mcamara/laravel-localization to handle the localization. it works fine for the routes I created myself :

Route::group(
    [
        'prefix' => LaravelLocalization::setLocale(),
        'middleware' => [ 'localeSessionRedirect', 'localizationRedirect', 'localeViewPath' ]
    ], function()
{
    Route::get('/', function () {
        return view('welcome');
    });

    Route::middleware(['auth:sanctum', 'verified'])->get('/dashboard', function () {
        return view('dashboard');
    })->name('dashboard');
});

But how can I set the group, prefix and middleware on the routes handled by Jetstream and Fortify?

[EDIT]

So after some suggestions from @TEFO, I'm trying to add a middleware to handle setting the locale. Added :

Fortify.php :

    'path' => '{lang}',
    'middleware' => ['web', 'setLang']

new middleware setLang :

class SetLang {
    /**
     * Handle an incoming request.
     *
     * @param \Illuminate\Http\Request $request
     * @param \Closure $next
     * @return mixed
     */
    public function handle(\Illuminate\Http\Request $request, Closure $next) {
        // $lang = 'en';
        // $request->attributes->add(['lang' => 'en']);
        $request->route()->setParameter('lang', 'en');
        // $request->request->set('lang', 'en');

        return $next($request);
    }
}

Added the middleware to $routeMiddleware.

I'm receiving this error when trying to reach http://mylaravel/en/login :

ErrorException
Missing required parameters for [Route: login] [URI: {lang}/login]. (View: /var/www/resources/views/auth/login.blade.php)
like image 970
Jeremy Belolo Avatar asked Dec 02 '22 08:12

Jeremy Belolo


2 Answers

Finally successfully nailed this. I simply disabled routes from Fortify and Jetstream, copied them over and shoved them inside my grouped prefix routes. Still using https://github.com/mcamara/laravel-localization but it should work anyway you want it - make your own system or whatever, as long as you control the routes you're good to go.

In JetstreamServiceProvider :

public function register() {
        Jetstream::ignoreRoutes();
    }

In FortifyServiceProvider :

public function register() {
        Fortify::ignoreRoutes();
    }

And copy over routes from Fortify vendor/laravel/fortify/routes/routes.php and Jetstream vendor/laravel/jetstream/routes/livewire.php (I guess adapt to Inertia if you're working with this) over to your web.php file, inside a route group with the prefix you need.

like image 170
Jeremy Belolo Avatar answered Dec 04 '22 00:12

Jeremy Belolo


I faced almost the same problem with the expection that i do not use mcamara/laravel-localization at the moment.

Based on the useful discussion above between @JeremyBelolo and @TEFO, the following solution worked for me:

  1. Added 'path' => '{locale}/my-secret-path' to config/fortify.php. As @JeremyBelolo and @ETO discussed, the support for that was recenlty added.
  2. Added my middleware before \Laravel\Jetstream\Http\Middleware\AuthenticateSession::class to the web $middlewareGroups
  3. Where my middleware set the locale app()->setLocale($locale); and the default {locale} url parameter URL::defaults(['locale' => $locale]); before passing the request deeper into the application.

Considering Jetstream I had to apply the same steps as @JeremyBelolo did, exept I didn't copy the jetsream/livewire routes but used the following inside the route group:

require base_path('vendor/laravel/jetstream/routes/livewire.php');

Now I can access {locale}/my-secret-path/login where {locale} is a supported locale for my site.

UPDATE [Fortify config option changed]:

The path fortify config option changed to prefix. Thus in config/fortify.php the following key should be used:

'prefix' => '{locale}/my-secret-path'

like image 40
marcell Avatar answered Dec 03 '22 23:12

marcell