AppapiController.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <?php
  2. /**
  3. * FecShop file.
  4. *
  5. * @link http://www.fecshop.com/
  6. * @copyright Copyright (c) 2016 FecShop Software LLC
  7. * @license http://www.fecshop.com/license/
  8. */
  9. namespace fecshop\app\appapi\modules;
  10. use Yii;
  11. use yii\filters\auth\CompositeAuth;
  12. use yii\filters\auth\HttpBasicAuth;
  13. use yii\filters\auth\HttpBearerAuth;
  14. use fecshop\yii\filters\auth\AppapiQueryParamAuth;
  15. use yii\rest\Controller;
  16. use yii\web\Response;
  17. use yii\filters\RateLimiter;
  18. /**
  19. * @author Terry Zhao <2358269014@qq.com>
  20. * @since 1.0
  21. */
  22. class AppapiController extends Controller
  23. {
  24. public $blockNamespace;
  25. public $enableCsrfValidation = false ;
  26. public function init()
  27. {
  28. parent::init();
  29. Yii::$app->user->enableSession = false;
  30. }
  31. public function behaviors()
  32. {
  33. $behaviors = parent::behaviors();
  34. $behaviors['contentNegotiator']['formats']['text/html'] = Response::FORMAT_JSON;
  35. //$behaviors['contentNegotiator']['formats']['text/html'] = Response::FORMAT_JSON;
  36. return $behaviors;
  37. }
  38. /**
  39. * get current block
  40. * you can change $this->blockNamespace.
  41. */
  42. public function getBlock($blockName = '')
  43. {
  44. if (!$blockName) {
  45. $blockName = $this->action->id;
  46. }
  47. if (!$this->blockNamespace) {
  48. $this->blockNamespace = Yii::$app->controller->module->blockNamespace;
  49. }
  50. if (!$this->blockNamespace) {
  51. throw new \yii\web\HttpException(406, 'blockNamespace is empty , you should config it in module->blockNamespace or controller blockNamespace ');
  52. }
  53. $relativeFile = '\\'.$this->blockNamespace;
  54. $relativeFile .= '\\'.$this->id.'\\'.ucfirst($blockName);
  55. //查找是否在rewriteMap中存在重写
  56. $relativeFile = Yii::mapGetName($relativeFile);
  57. return new $relativeFile();
  58. }
  59. }