How to create URL with dymanic query parameters ?

I want to create link to the currently visited page but change the application language.

For example I am on article page that is displaying one article on Italian language, and I want to allow users to change language to English but to stay on that article. How can I do this ?

I have tried this, but it is not working:


<?= HTML::a('en', UrlHelper::createLocalizedUrl('en'), ['class' => '']) ?>


    public static function createLocalizedUrl($language)

    {

        $request = Yii::$app->request;

        $pathInfo[] = $request->getPathInfo();

        $params = $request->getQueryParams();


        $url = array_push($pathInfo, $params);


        Yii::$app->language = $language;


        return $url;

    }

I am also using https://github.com/codemix/yii2-localeurls

Any help please ? The way http://www.yiiframework.com/doc-2.0/yii-web-urlmanager.html#createUrl()-detail works, give me headache.

There are many ways. one way is:

if you have the language change links in the top of the page, code in your views/layouts/main.php file the links to send by post 2 parameters.

  1. the new language

  2. the current url of the page (You can obtain it with Yii::$app->request->absoluteUrl etc.)

in the controller, your action should accept post, change the language using 1st param and then redirect to the url param.