yii2 Countermeasure events



  • There are two modules - Comments and Pages with their structure, i.e. implementation is not standard as default in Yii2. Accordingly, two controllers

    class CommentsController extends BaseController
    {            
        public function actionIndex()
        {
            return $this->render('index');
        }
    }
    

    Event::on(PagesController::className(), PagesController::EVENT_GET_COMMENTS,
    function ($event) {
    echo 'comments';
    });

    Well, that's the same for Pages.

    class PagesController extends BaseController
    {
    const EVENT_GET_COMMENTS = '';

    public function actionIndex()
    {
        $this->trigger(self::EVENT_GET_COMMENTS);
    
        return $this->render('index');
    }
    

    }

    In theory, when you call the counterall Pages, index the event must be made EVENT_GET_COMMENTS and withdraw commentsbut it's not happening:



  • You have three ways:

    No1 (global):

    1. Create with the necessary logic
    2. Add to the configuration file

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

      And don't forget. 'bootstrap' => ['log','myComponent']

    No2:

    To inherit the method in the counteraller.

    public function beforeAction($event)
    {
        // ваш код
        return parent::beforeAction($event);
    }
    

    No3:

    1. Behaviour: http://www.yiiframework.com/doc-2.0/guide-concept-behaviors.html , see how

    2. Add written behaviour in the counterer, e.g. AccessControl




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2