EventBootstrap.php 965 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: wushuiyong
  5. * Date: 15/8/14
  6. * Time: 下午5:41
  7. */
  8. namespace app\components;
  9. use Yii;
  10. use yii\base\BootstrapInterface;
  11. use yii\web\NotFoundHttpException;
  12. use yii\helpers\Url;
  13. class EventBootstrap implements BootstrapInterface
  14. {
  15. public function bootstrap($app) {
  16. $this->event();
  17. }
  18. public function event() {
  19. Yii::$app->on(\yii\base\Application::EVENT_BEFORE_ACTION, function ($event) {
  20. $aid = $event->action->id;
  21. $cid = $event->action->controller->id;
  22. if (Yii::$app->user->id) return true;
  23. if ($cid == 'site') {
  24. return true;
  25. }
  26. if (!Yii::$app->request->getIsAjax()) {
  27. Yii::$app->response->redirect('/site/login');
  28. Yii::$app->end();
  29. }
  30. $event->isValid = false;
  31. throw new \Exception('请先登录,再操作:)');
  32. });
  33. }
  34. }