Difference between #72 and #73 of
Yii v2 snippet guide

Revision #73 has been created by rackycz on Sep 20, 2019, 3:06:33 PM with the memo:

« previous (#72) next (#74) »


Title unchanged

Yii v2 for beginners

Category unchanged


Yii version unchanged


Tags unchanged


Content changed

// ...

Now in the top-right corner you can see a new drop-down-list with list of 2 languages. If one is selected, action "site/setLang" is used so we have to create it in SiteController.

*Note that this approach will always redirect user to the new action and his work will be lost. Nevertheless this approach is very simple so I am using it in small projects. More complex projects may require an ajax call when language is changed and then updating texts using javascript so reload is not needed and user's work is preserved. But I expect that when someone opens the web, he/she sets the language immediately and then there is no need for further changes.*
The setLang looks like this:
    public function actionSetLang($langID = "") {
        if ($langID && trim($langID) !== "" && in_array($langID, Yii::$app->params["allowedLanguages"])) {
            Yii::$app->session->set('langID', $langID);
        return $this->redirect(['site/index']);
Access rights --- ... text ...
4 0
Viewed: 44 800 times
Version: 2.0
Category: Tutorials
Written by: rackycz
Last updated by: rackycz
Created on: Sep 19, 2019
Last updated: 23 hours ago
Update Article


View all history