skip to Main Content

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


  1. Chosen as BEST ANSWER

    I have done these updates 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->alias([
                    'locale'=>AppHttpMiddlewareSetLanguage::class
                ]);
            })
            ->withExceptions(function (Exceptions $exceptions) {
        
            })->create();
    

    web.php

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

    As long as your localized routes are inside the wrapper, Livewire routes won't be interrupted with prefixes.


  2. One of the solution you can do is

    1. You can just use a callback to register your routes:
    ->withRouting(
        commands: __DIR__.'/../routes/console.php',
        using: function () {
            Route::middleware(['web','locale'])->group(base_path('routes/web.php'))
        }
    )->withMiddleware(function (Middleware $middleware) {
        $middleware->alias([
            'locale' => SetLanguage::class
        ])
    })
    

    Take note that the health (/up) route will not be register so you can just also define it inside the callback.

    Routing Customization

    1. Wrap the routes defined in the web.php inside the Route::middleware()->group(fn () => ...)
    Route::middleware(SetLanguage::class)->group(function () {
       // register routes here
    })
    
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search