NoCSRFBehavior.php 650 B

12345678910111213141516171819202122232425262728293031323334
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: chenkuan
  5. * Date: 2017/12/19
  6. * Time: 下午5:27
  7. */
  8. namespace wechat\models\behaviors;
  9. use yii\base\ActionEvent;
  10. use yii\base\Behavior;
  11. use yii\web\Controller;
  12. class NoCSRFBehavior extends Behavior
  13. {
  14. /** @var ActionEvent */
  15. public $actions = [];
  16. public $controller;
  17. public function events()
  18. {
  19. return [Controller::EVENT_BEFORE_ACTION => 'beforeAction'];
  20. }
  21. public function beforeAction($event)
  22. {
  23. $action = $event->action->id;
  24. if (in_array($action, $this->actions)) {
  25. $this->controller->enableCsrfValidation = false;
  26. }
  27. }
  28. }