Laravel lang slug in url



PHP Snippet 1:

Route::prefix('{locale}')->group(function ($locale) {

    if (array_key_exists($locale, Config::get('languages'))) {
        Session::put('applocale', $locale);
    }
    // else return 404 
    Route::get('your-slug', function () {    
    });
});

PHP Snippet 2:

Route::middleware(App::getLocale())->group(function () {
    Route::get('/', function () {
        // Uses first & second Middleware
    });

    Route::get('user/profile', function () {
        // Uses first & second Middleware
    });
});

PHP Snippet 3:

$locale = $request->segment(1);

if (in_array($locale, config('app.locales'))) {
     \App::setLocale($locale);
            return $next($request);
} else {
    //
}