'Laravel lang slug in url

I've followed this article to add multi language into my app and it's working fine, the only issue i need to solve is that languages slug won't add to the url.

Example

based on the article above i get my page in English like:

www.myapp.co/my-slug

and in another language the same (however translation will placed)

what i want is like:

www.myapp.co/en/my-slug

&

www.myapp.co/id/my-slug

etc.

Any idea?



Solution 1:[1]

Solved

I used this tutorial and it gets me what I needed

Solution 2:[2]

Perhaps you can try my answer here, where I used mcamara's Laravel Localization package to achieve this. It was painless for me :)

Solution 3:[3]

Change your route using prefix

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 () {    
    });
});

Solution 4:[4]

May be you can use App::getLocale() as route group prefix and use that to choose language .

In routing

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

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

Retrieve the request parameters in middleware and if require In your middleware you can put something like a conditional and reconstruct the url if required and then redirect it to the newly constructed url.

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

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 sykez
Solution 3 Jamsheer Mohammed
Solution 4