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

iqlas's avatar
Level 4

Append query string to URL

I am working on multi lingual application in Laravel (5.7)

I have a middleware Locale which is applied for all the web routes group and it checks if the locale is set for application and sets if not. Here to make the URL unique for each language, I want to add query string ?lang=en or as required.

I used the following in Locale middleware

public function handle($request, Closure $next)
    {
        $lang=$request->query('lang') ? $request->query('lang') : 'en';
        app()->setLocale($lang);
        array_merge($request->query(), ['lang' => $lang]);
        return $next($request);
    }

The language is being set as required but the URL doesn't show the merged query string like /?lang=en

Can anyone suggest or guide me if there is a better way to implement this?

0 likes
4 replies
PatrickSJ's avatar

Out of curiosity, are users required to signed in? If so you could simply fetch the signed in user's locale instead. No need for getting it from the request.

iqlas's avatar
Level 4

@PATRICKSJ - @patricksj some of the pages require login & some don't. User locale would be a great add too. Thanks for suggestion.

The primary reason I wanted to generate urls with language defined in query string is to generate SEO friendly unique URL's for each language per page.

testlara's avatar

@IQLAS - How to track links (count clicks on links more authenticately) in websites ...How many times are they clicked ... that means if a user click so many times in a particular times it will be counted once ..is Google Tag Manager best solutions ? How to implement it on laravel projects more authenticately with less database volumes ... Suppose think a facebook-page link is in my webapp ...I want to track the no of clicks (it would be better if it be unique click for a particular time say for 24 hrs i.e if a user clicks so many times within 24 hours it will be counted one ) Please if you have any code ...

PatrickSJ's avatar

@IQLAS - How are you setting the locale for users on the webpage and tracking it? You could also set the session data to track the locale for users who aren't logged in.

However, for SEO optimization this is outside my area on knowledge. Personally I would do something like www.example.com/es/page or www.example.com/en/page to control the locale. They all direct to the same route, but the locale is set in the path instead of the query string.

The Locale middleware can parse the route to set the locale based on the path.

Edit: I forgot about the accept-language that you can set. Go with that instead.

Please or to participate in this conversation.