Execute my code before any action of any controller

后端 未结 5 1580
我寻月下人不归
我寻月下人不归 2020-12-05 00:21

I would like to check if my user have filled certain fields in his profile before he can access any action of any controller. For example

if(empty(field1) &         


        
相关标签:
5条回答
  • 2020-12-05 00:52

    Just add in config file into $config array:

        'on beforeAction' => function ($event) {
               echo "Hello";
        },
    
    0 讨论(0)
  • 2020-12-05 00:54

    Create a new controller

    namespace backend\components;
    class Controller extends \yii\web\Controller {
        public function beforeAction($event)
        {
            ..............
            return parent::beforeAction($event);
        }
    }
    

    All your controllers should now extend backend\components\Controller and not \yii\web\Controller. with this, you should modify every controller. I would go for this solution.

    I believe you might also replace 1 class with another (so no change to any controller necessary), something like

    \Yii::$classMap = array_merge(\Yii::$classMap,[
                    '\yii\web\Controller'=>'backend\components\Controller',
                ]);
    

    See more details here: http://www.yiiframework.com/doc-2.0/guide-tutorial-yii-integration.html and I took the code from here: https://github.com/mithun12000/adminUI/blob/master/src/AdminUiBootstrap.php

    you can put this in your index.php file. However, make sure you document this change very well as somebody that will come and try to debug your code will be totally confused by this.

    0 讨论(0)
  • 2020-12-05 00:54

    Or, https://github.com/yiisoft/yii2/blob/master/docs/guide/security-authorization.md use RBAC, to restrict access to controllers actions one at a time based on rules. Why would you want to restrict access to controller actions based on user fields is beyond me. You will not be able to access anything (including the login form) if you put a restriction there.

    0 讨论(0)
  • 2020-12-05 00:57

    Just i think this code on config file can help you:

    'on beforeAction' => function ($event) {
          // To log all request information
    },
    'components' => [
        'response' => [
            'on beforeSend' => function($event) {
                // To log all response information
            },
        ],
    ];
    
    0 讨论(0)
  • 2020-12-05 01:01

    In case you need to execute a code before every controller and action, you can do like below:

    1 - Add a component into your components directory, for example(MyGlobalClass):

    namespace app\components;
    class MyGlobalClass extends \yii\base\Component{
        public function init() {
            echo "Hi";
            parent::init();
        }
    }
    

    2 - Add MyGlobalClass component into your components array in config file:

    'components' => [
        'MyGlobalClass'=>[
            'class'=>'app\components\MyGlobalClass'
         ],
         //other components
    

    3 - Add MyGlobalClass into bootstarp array in config file:

    'bootstrap' => ['log','MyGlobalClass'],
    

    Now, you can see Hi before every action.

    Please note that, if you do not need to use Events and Behaviors you can use \yii\base\Object instead of \yii\base\Component

    0 讨论(0)
提交回复
热议问题