I have a problem with setting Laravel 11 locale and livewire 3 working together. The problem is that the Livewire post request ‘POST: livewire/update’ fails, I’m getting a 404 response but if I disable the locale prefix and Middleware, livewire works fine.
bootstrapapp.php
return Application::configure(basePath: dirname(__DIR__))
->withRouting(
web: __DIR__.'/../routes/web.php',
commands: __DIR__.'/../routes/console.php',
health: '/up',
then: function () {
$locale = request()->segment(1);
if(in_array($locale,config('app.locales'))){
Route::middleware('web')
->prefix($locale)
->name('webroutes')
->group(base_path('routes/web.php'));
}
},
)
->withMiddleware(function (Middleware $middleware) {
$middleware->append(AppHttpMiddlewareSetLanguage::class);
})
->withExceptions(function (Exceptions $exceptions) {
})->create();
SetLanguage.php Middleware
{
/**
* Handle an incoming request.
*
* @param Closure(IlluminateHttpRequest): (SymfonyComponentHttpFoundationResponse) $next
*/
public function handle(Request $request, Closure $next): Response
{
$locale = $request->segment(1);
if (!in_array($locale, config('app.locales'))) {
return redirect(url(getCurrentUrlWithLocale(config('app.fallback_locale'))));
}
app()->setLocale($locale);
return $next($request);
}
}
After troubleshooting I found out that the Liveware post request doesn’t work with the locale language prepended on the URL e.g. domain.test/fr/todo. Can someone help me to either exclude post ‘livewire/update’ from locale functionalities or make the post work request work with a prepend locale URL?
2
Answers
I have done these updates app.php
web.php
As long as your localized routes are inside the wrapper, Livewire routes won't be interrupted with prefixes.
One of the solution you can do is
Take note that the
health (/up)
route will not be register so you can just also define it inside the callback.Routing Customization
web.php
inside theRoute::middleware()->group(fn () => ...)