Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Mike4x's avatar
Level 1

Laravel Livewire post request does not work with localization web URL prefix

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.

bootstrap/app.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(\App\Http\Middleware\SetLanguage::class); }) ->withExceptions(function (Exceptions $exceptions) {

})->create();

SetLanguage.php Middleware

{ /** * Handle an incoming request. * * @param \Closure(\Illuminate\Http\Request): (\Symfony\Component\HttpFoundation\Response) $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);
}

}

In short, livewire doesn't work with URL prepends, it returns 404. This happened after following this Localization Episode from the Laravel Cookbook series. https://laracasts.com/series/laravel-cookbook/episodes/11

Can someone help me figure out how to make livewire routes like 'livewire/update' not be affected by URL prepends like language ie domain.test/en/todo Or modify the livewire URLs to work locale prepends, but the former the better.

0 likes
1 reply
LaryAI's avatar
Level 58

To solve the issue of Livewire not working with URL prefixes for localization, you need to ensure that Livewire's routes are not affected by the locale prefix. One way to achieve this is by excluding Livewire routes from the locale prefix middleware.

Here's a step-by-step solution:

  1. Update the web.php routes file to ensure Livewire routes are not prefixed by the locale:
use Illuminate\Support\Facades\Route;
use Livewire\Livewire;

// Define your localized routes
Route::middleware('web')
    ->prefix('{locale}')
    ->where(['locale' => '[a-zA-Z]{2}'])
    ->group(function () {
        // Your localized routes go here
        Route::get('/', function () {
            return view('welcome');
        });
    });

// Define Livewire routes without locale prefix
Livewire::routes();
  1. Modify the SetLanguage middleware to handle the locale correctly and ensure it doesn't interfere with Livewire routes:
namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\Response;

class SetLanguage
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure(\Illuminate\Http\Request): (\Symfony\Component\HttpFoundation\Response)  $next
     * @return \Symfony\Component\HttpFoundation\Response
     */
    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);
    }
}
  1. Ensure your bootstrap/app.php is correctly configured to handle the locale prefix and middleware:
use Illuminate\Foundation\Application;
use Illuminate\Routing\Middleware\SubstituteBindings;
use Illuminate\Routing\Router;

$app = Application::configure(basePath: dirname(__DIR__))
    ->withRouting(
        web: __DIR__.'/../routes/web.php',
        commands: __DIR__.'/../routes/console.php',
        health: '/up',
        then: function (Router $router) {
            $locale = request()->segment(1);
            if (in_array($locale, config('app.locales'))) {
                $router->middleware('web')
                    ->prefix($locale)
                    ->name('webroutes')
                    ->group(base_path('routes/web.php'));
            }
        }
    )
    ->withMiddleware(function (Middleware $middleware) {
        $middleware->append(\App\Http\Middleware\SetLanguage::class);
    })
    ->withExceptions(function (Exceptions $exceptions) {
        // Exception handling configuration
    })
    ->create();

return $app;
  1. Update your config/app.php to include the supported locales:
return [
    // Other configurations...

    'locales' => ['en', 'es', 'fr'], // Add your supported locales here

    'fallback_locale' => 'en',
];

By following these steps, you ensure that Livewire routes are not affected by the locale prefix, allowing them to function correctly while still supporting localized routes for the rest of your application.

Please or to participate in this conversation.