PaidController.php 1.7 KB

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