PaidController.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. <?php
  2. namespace api\modules\v1\controllers;
  3. use yii\filters\auth\CompositeAuth;
  4. use yii\filters\auth\HttpBasicAuth;
  5. use yii\filters\auth\HttpBearerAuth;
  6. use yii\filters\auth\QueryParamAuth;
  7. use yii\helpers\ArrayHelper;
  8. use yii\filters\VerbFilter;
  9. class PaidController extends \yii\rest\Controller
  10. {
  11. public function behaviors()
  12. {
  13. return ArrayHelper::merge(parent::behaviors(), [
  14. 'authenticator' => [
  15. //使用ComopositeAuth混合认证
  16. 'class' => CompositeAuth::className(),
  17. 'optional' => [
  18. 'info',//无需access-token的action
  19. ],
  20. 'authMethods' => [
  21. HttpBasicAuth::className(),
  22. HttpBearerAuth::className(),
  23. [
  24. 'class' => QueryParamAuth::className(),
  25. 'tokenParam' => 'access-token',
  26. ]
  27. ]
  28. ],
  29. 'verbs' => [
  30. 'class' => VerbFilter::className(),
  31. 'actions' => [
  32. 'info' => ['GET'],
  33. ],
  34. ],
  35. ]);
  36. }
  37. /**
  38. * 访问路由 /v1/paids 或/v1/paid/index (p.s如果入口在frontend/web/api/index.php则还需在前加上api)
  39. *
  40. * @return array
  41. */
  42. public function actionIndex()
  43. {
  44. return ["我是v1 paid/index 需要access-token才能访问的接口"];
  45. }
  46. /**
  47. * 访问路由 /v1/paid/info (p.s如果入口在frontend/web/api/index.php则还需在前加上api)
  48. *
  49. * @return array
  50. */
  51. public function actionInfo()
  52. {
  53. return ["我是v1 paid/info 我不需要access-token也能访问"];
  54. }
  55. }