Difference between #77 and #78 of
Yii v2 snippet guide

Revision #78 has been created by rackycz on Sep 20, 2019, 3:21:43 PM with the memo:

edit
« previous (#77) next (#79) »

Changes

Title unchanged

Yii v2 for beginners

Category unchanged

Tutorials

Yii version unchanged

2.0

Tags unchanged

tutorial,beginner,yii2

Content changed

[...]
}
return $this->redirect(['site/index']);
}
```

As you can see when the language is changed, redirection to site/index happens. Also mind that we are not modifying the attribute from config/web.php using Yii::$app->language, but we are saving the value into the session. The reason is that PHP deletes memory after every click, only session is kept. We then can use the value in other controllers and views
like this:
 
 
```php
 
Yii::$app->language = Yii::$app->session->get('langID');
 
```
 
 
using method beforeAction:
 
 
```php
 
    public function beforeAction($action) {
 
 
        if (!parent::beforeAction($action)) {
 
            return false;
 
        }
 
 
        Yii::$app->language = Yii::$app->session->get('langID');
 
 
        return true;
 
    }
 
```
 
 
.. or you can create one parent-controller named for example BaseController. All other controllers will extend it.
 
 
```php
 
<?php
 
 
namespace app\controllers;
 
 
use Yii;
 
use yii\web\Controller;
 
 
class BasicController extends Controller {
 
 
    public function beforeAction($action) {
 
 
        if (!parent::beforeAction($action)) {
 
            return false;
 
        }
 
 
        Yii::$app->language = Yii::$app->session->get('langID');
 
 
        return true;
 
    }
 
 
}
 
 
```
Access rights --- ... text ...
7 0
4 followers
Viewed: 258 022 times
Version: 2.0
Category: Tutorials
Written by: rackycz
Last updated by: rackycz
Created on: Sep 19, 2019
Last updated: 6 months ago
Update Article

Revisions

View all history