BaseController.php 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. <?php
  2. namespace addons\SmartForm\backend\controllers;
  3. use Yii;
  4. use common\controllers\AddonsController;
  5. use yii\web\UnauthorizedHttpException;
  6. /**
  7. * 默认控制器
  8. *
  9. * Class DefaultController
  10. * @package addons\SmartForm\backend\controllers
  11. */
  12. class BaseController extends AddonsController
  13. {
  14. /**
  15. * @var string
  16. */
  17. // public $layout = "@addons/SmartForm/backend/views/layouts/main";
  18. /**
  19. * @param $action
  20. * @return bool
  21. * @throws UnauthorizedHttpException
  22. * @throws \yii\web\BadRequestHttpException
  23. */
  24. public function beforeAction($action)
  25. {
  26. try {
  27. if (!parent::beforeAction($action)) {
  28. return false;
  29. }
  30. return true;
  31. } catch (UnauthorizedHttpException $e) {
  32. $request = \Yii::$app->request;
  33. $is_ajax = $request->isAjax;
  34. if ($is_ajax) {
  35. \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
  36. \Yii::$app->response->data = ['code' => 1, 'msg' => $e->getMessage()];
  37. \Yii::$app->response->send();
  38. return false;
  39. } else {
  40. $this->layout = 'exception';
  41. return $this->render('index');
  42. // throw new UnauthorizedHttpException($e->getMessage());
  43. }
  44. }
  45. }
  46. }