1

I'm trying to use Laravel Auth::routes() encapsulated within a prefix group for localization purposes:

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

In my views I now create routes providing the current language like this route('password.confirm', app()->getLocale())

But when I try to use the "forgot password" function, an exception is thrown. I believe this is caused because Laravel internally creates a password reset link, using a named route without passing along the current language parameter.

Illuminate\Routing\Exceptions\UrlGenerationException
Missing required parameter for [Route: password.reset] 
[URI: {locale}/password/reset/{token}] [Missing parameter: locale].

Is it possible to use Auth::routes() and inject the missing "locale" parameter globally in some way? Or what would be the suggested way to do it without rewriting Laravels authentication methods?

1 Answer 1

1

I have found a solution. Thanks to this answer https://stackoverflow.com/a/49380950/9405862 it inspired me to add a middleware to my route group which adds the missing parameter to the URLs:

Route::group([
    'middleware' => HandleRouteLang::class,
    'prefix' => '{locale}',
    'where' => ['locale' => '[a-zA-Z]{2}']
], function () { 
    Auth::routes();
});

My middleware now looks like this:

<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Contracts\Routing\UrlGenerator;

class HandleRouteLang
{
    private $url;

    public function __construct(UrlGenerator $url)
    {
        $this->url = $url;
    }

    public function handle($request, Closure $next)
    {
        // Set current language by locale parameter within URLs
        if ($request->route("locale")) {
            app()->setlocale($request->route("locale"));
        }

        // Set default language value for routes created by named routes
        $this->url->defaults([
            'locale' => app()->getLocale(),
        ]);

        return $next($request);
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.