Yii v2 snippet guide

Comparing #113 with #114

Revision #114 was created by rackycz rackycz on Sep 27, 2019, 7:27:01 AM.

edit

Content

[...]
As you can see in the snippet above, other controllers must contain row "use app\controllers\BaseController" + "extends BaseController"


**Access rights**
---
... text ...
 
 
**Nice URLs**
 
---
 
... text ...
 
 
**How to redirect web to subfolder /web**
 
---
 
... text ...
Every controller can allow different users/guests to use different actions. Method behaviors() can be used to do this. If you generate the controller using GII the method will be present and you will just add the "access-part"  like this:
 
 
```php
 
public function behaviors() {
 
  return [
 
    // ...
 
    'access' => [
 
      'class' => AccessControl::className(),
 
      'rules' => [
 
        [
 
          'allow' => true,
 
          'roles' => ['@'], // logged in users
 
          // 'roles' => ['?'], // guests
 
          'matchCallback' => function ($rule, $action) {
 
            // all logged in users are redirected to some other page
 
            // just for demonstration of matchCallback
 
            return $this->redirect('index.php?r=user/create');
 
          }
 
        ],
 
      ],
 
      // All guests are redirected to site/index in current controller:
 
      'denyCallback' => function($rule, $action) {
 
        Yii::$app->response->redirect(['site/index']);
 
      },
 
    ],
 
  ];
 
}
 
```
 
 
Details can be found here [https://www.yiiframework.com/doc/guide/2.0/en/security-authorization](https://www.yiiframework.com/doc/guide/2.0/en/security-authorization).
 
 
**Nice URLs**
 
---
 
... text ...
 
 
**How to redirect web to subfolder /web**
 
---
 
... text ...
 
 
**Auto redirection from login to desired URL **
 
---
 
... text ...
 
 
**What to change when exporting to the Interne**
 
---
 
- Delete file web/index-test.php
 
- In file web/index.php comment you 2 first lines containing YII_DEBUG + YII_ENV