1
0

BaseActiveController.php 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. <?php
  2. namespace frontend\controllers;
  3. use Yii;
  4. use yii\web\Controller;
  5. use yii\filters\auth\CompositeAuth;
  6. use yii\filters\auth\QueryParamAuth;
  7. class BaseActiveController extends Controller
  8. {
  9. public $modelClass = 'common\models\user';
  10. public $post = null;
  11. public $get = null;
  12. public $user = null;
  13. public $userId = null;
  14. public function init()
  15. {
  16. parent::init();
  17. Yii::$app->user->enableSession = false;
  18. }
  19. public function behaviors()
  20. {
  21. $behaviors = parent::behaviors();
  22. $behaviors['authenticator'] = [
  23. 'class' => CompositeAuth::className(),
  24. 'authMethods' => [
  25. // HttpBasicAuth::className(),
  26. // HttpBearerAuth::className(),
  27. QueryParamAuth::className(),
  28. ],
  29. ];
  30. // 数据返回类型设置
  31. //$behaviors['contentNegotiator']['formats']['application/json'] = 'json';
  32. //$behaviors['contentNegotiator']['formats']['application/xml'] = 'json';
  33. return $behaviors;
  34. }
  35. public function beforeAction($action)
  36. {
  37. parent::beforeAction($action);
  38. $this->post = yii::$app->request->post();
  39. $this->get = yii::$app->request->get();
  40. $this->user = yii::$app->user->identity;
  41. $this->userId = Yii::$app->user->id;
  42. return $action;
  43. }
  44. }