AppserverController.php 1.9 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\appserver\modules;
  10. use yii\rest\Controller;
  11. use Yii;
  12. use yii\web\Response;
  13. /**
  14. * @author Terry Zhao <2358269014@qq.com>
  15. * @since 1.0
  16. */
  17. class AppserverController extends Controller
  18. {
  19. public $blockNamespace;
  20. public function init()
  21. {
  22. parent::init();
  23. Yii::$service->page->translate->category = 'appserver';
  24. // 如果用户登录,会在header中传递access-token,这个函数就会登录用户。
  25. Yii::$service->customer->loginByAccessToken();
  26. }
  27. public function behaviors()
  28. {
  29. $behaviors = parent::behaviors();
  30. $behaviors['contentNegotiator']['formats']['text/html'] = Response::FORMAT_JSON;
  31. $behaviors["corsFilter"] = [
  32. 'class' => \yii\filters\Cors::className(),
  33. 'cors' => Yii::$service->helper->appserver->getCors(),
  34. ];
  35. return $behaviors;
  36. }
  37. /**
  38. * get current block
  39. * you can change $this->blockNamespace.
  40. */
  41. public function getBlock($blockName = '')
  42. {
  43. if (!$blockName) {
  44. $blockName = $this->action->id;
  45. }
  46. if (!$this->blockNamespace) {
  47. $this->blockNamespace = Yii::$app->controller->module->blockNamespace;
  48. }
  49. if (!$this->blockNamespace) {
  50. throw new \yii\web\HttpException(406, 'blockNamespace is empty , you should config it in module->blockNamespace or controller blockNamespace ');
  51. }
  52. $viewId = $this->id;
  53. $viewId = str_replace('/', '\\', $viewId);
  54. $relativeFile = '\\'.$this->blockNamespace;
  55. $relativeFile .= '\\'.$viewId.'\\'.ucfirst($blockName);
  56. //查找是否在rewriteMap中存在重写
  57. $relativeFile = Yii::mapGetName($relativeFile);
  58. return new $relativeFile();
  59. }
  60. }