1
0

BaseController.php 1009 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. <?php
  2. namespace backend\controllers;
  3. use components\Exception;
  4. use yii\helpers\ArrayHelper;
  5. use yii\web\Controller;
  6. /**
  7. * Class BaseController
  8. * @package backend\controllers
  9. */
  10. class BaseController extends Controller
  11. {
  12. /**
  13. * init
  14. */
  15. public function init()
  16. {
  17. \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
  18. parent::init();
  19. }
  20. /**
  21. * behaviors
  22. * @return array
  23. */
  24. public function behaviors()
  25. {
  26. return ArrayHelper::merge(parent::behaviors(), [
  27. 'bearerAuth' => [
  28. 'class' => \common\filters\auth\BearerAuth::className(),
  29. //'class' => \yii\filters\auth\HttpBearerAuth::className(),
  30. 'optional' => [
  31. 'token',
  32. 'signature',
  33. 'test',
  34. ],
  35. ]
  36. ] );
  37. }
  38. /**
  39. * beforeAction
  40. * @return bool
  41. * @throws Exception
  42. */
  43. public function beforeAction($action)
  44. {
  45. try {
  46. return parent::beforeAction($action);
  47. } catch (\Exception $e) {
  48. throw new Exception(isset($e->statusCode) ? $e->statusCode : $e->getCode(), $e->getMessage());
  49. }
  50. }
  51. }