Laravel-localization middlewares now support route exceptions

Konstantin KomelinKonstantin Komelin

We usually apply laravel-localization middlewares to the entire web route group. It means that laravel-localization package redirects broadcasting/auth route to en/broadcasting/auth, which crashes Event Broadcasting.

The problem is related to the fact that Laravel Echo Server does not support redirects (HTTP statuses other than 200) of the Laravel authorization service broadcasting/auth.

It is probably possible to improve Laravel Echo Server in order to support redirects, but from my point of view it is easier to solve the problem from the Laravel side.

Good news is that the latest version of the mcamara/laravel-localization includes support for route exceptions ($except property). I'll quickly show you how to use it.

In your app/Http/Middleware folder create a new middleware class and inherit it from one of the laravel-localization middlewares. For example:

<?php
namespace App\Http\Middleware;
use \Mcamara\LaravelLocalization\Middleware\LaravelLocalizationRedirectFilter as McamaraLaravelLocalizationRedirectFilter;
class LaravelLocalizationRedirectFilter extends McamaraLaravelLocalizationRedirectFilter
{
    /**
     * The URIs that should not be redirected to their localized versions.
     *
     * @var array
     */
    protected $except = [
        'broadcasting/auth',
    ];
}

Then replace the original middleware with your new middleware class in app/Http/Kernel.php:

protected $middlewareGroups = [
        'web' => [
            //  'localizationRedirect'=> \Mcamara\LaravelLocalization\Middleware\LaravelLocalizationRedirectFilter::class,
            'localizationRedirect' => \App\Http\Middleware\LaravelLocalizationRedirectFilter::class,
        ],
];

You can apply the same principle to all laravel-localization middlewares. Enjoy!