It looks like the issue you're encountering with the 404 error is due to the way the URL is being generated in your Blade template. The problem is that the href attribute in your anchor tags is incorrectly appending the full route URL to the base URL, resulting in a malformed URL.
Here's how you can fix it:
-
Correct the URL generation in the Blade template: You should use the
routehelper function correctly to generate the URL. Ensure that you do not prepend a slash (/) before theroutefunction call, asroutealready generates a complete URL. -
Ensure the session locale is used: Make sure that the application is actually using the locale stored in the session. This typically involves setting the locale in a middleware.
Here's the corrected version of your Blade template and additional steps to ensure the locale changes take effect:
Updated Blade Template (header.blade.php)
<div class="dropdown">
<div tabindex="0" role="button" class="btn btn-ghost">
{{ __('original.language') }}
</div>
<ul tabindex="0" class="menu menu-sm dropdown-content mt-3 p-2 shadow bg-base-100 rounded-box w-52">
<li>
<a href="{{ route('locale', 'es') }}">
<div>
<x-flag-country-es class="w-6 h-6" />
</div>
</a>
</li>
<li>
<a href="{{ route('locale', 'en') }}">
<div>
<x-flag-country-uk class="w-6 h-6" />
</div>
</a>
</li>
<!-- Add as many languages as you need -->
</ul>
</div>
Middleware for Setting Locale
Create a middleware to set the application locale based on the session value.
-
Generate Middleware:
php artisan make:middleware LocaleMiddleware -
Implement Middleware (app/Http/Middleware/LocaleMiddleware.php):
namespace App\Http\Middleware; use Closure; use Illuminate\Http\Request; use App; class LocaleMiddleware { public function handle(Request $request, Closure $next) { if (session()->has('locale')) { App::setLocale(session()->get('locale')); } return $next($request); } } -
Register Middleware: Add the middleware to the
webmiddleware group inapp/Http/Kernel.php:protected $middlewareGroups = [ 'web' => [ // other middleware \App\Http\Middleware\LocaleMiddleware::class, ], ];
With these changes, your application should correctly change the locale based on the user's selection and redirect back without generating malformed URLs.