AppfrontController.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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\appfront\modules;
  10. use fec\controllers\FecController;
  11. use fec\helpers\CConfig;
  12. use Yii;
  13. use yii\base\InvalidValueException;
  14. /**
  15. * @author Terry Zhao <2358269014@qq.com>
  16. * @since 1.0
  17. * Appfront 入口的controller的基类
  18. */
  19. class AppfrontController extends FecController
  20. {
  21. public $blockNamespace;
  22. /**
  23. * init theme component property : $fecshopThemeDir and $layoutFile
  24. * $fecshopThemeDir is appfront base theme directory.
  25. * layoutFile is current layout relative path.
  26. */
  27. public function init()
  28. {
  29. /**
  30. * 如果模板路径没有配置,则配置模板路径
  31. */
  32. if (!Yii::$service->page->theme->fecshopThemeDir) {
  33. Yii::$service->page->theme->fecshopThemeDir = Yii::getAlias(CConfig::param('appfrontBaseTheme'));
  34. }
  35. /**
  36. * 如果layout文件没有配置,则配置layout文件
  37. */
  38. if (!Yii::$service->page->theme->layoutFile) {
  39. Yii::$service->page->theme->layoutFile = CConfig::param('appfrontBaseLayoutName');
  40. }
  41. /*
  42. * set i18n translate category.
  43. */
  44. Yii::$service->page->translate->category = 'appfront';
  45. /*
  46. * 自定义Yii::$classMap,用于重写
  47. */
  48. }
  49. /**
  50. * @param $blockName | String
  51. * get current block
  52. * 这个函数的controller中得到block文件,譬如:
  53. * cms模块的ArticleController的actinIndex()方法中使用$this->getBlock()->getLastData()方法,
  54. * 对应的是cms/block/article/Index.php里面的getLastData(),
  55. * 也就是说,这个block文件路径和controller的路径有一定的对应关系
  56. * 这个思想来自于magento的block。
  57. */
  58. public function getBlock($blockName = '')
  59. {
  60. if (!$blockName) {
  61. $blockName = $this->action->id;
  62. }
  63. if (!$this->blockNamespace) {
  64. $this->blockNamespace = Yii::$app->controller->module->blockNamespace;
  65. }
  66. if (!$this->blockNamespace) {
  67. throw new \yii\web\HttpException(406, 'blockNamespace is empty , you should config it in module->blockNamespace or controller blockNamespace ');
  68. }
  69. $viewId = $this->id;
  70. $viewId = str_replace('/', '\\', $viewId);
  71. $relativeFile = '\\'.$this->blockNamespace;
  72. $relativeFile .= '\\'.$viewId.'\\'.ucfirst($blockName);
  73. //查找是否在rewriteMap中存在重写
  74. $relativeFile = Yii::mapGetName($relativeFile);
  75. return new $relativeFile();
  76. }
  77. /**
  78. * @param $view|string , (only) view file name ,by this module id, this controller id , generate view relative path.
  79. * @param $params|Array,
  80. * 这个是fecshop重写的render函数,根据fecshop的多模板机制
  81. * 首先在高级别的模板中找view文件,如果找不到,按照模板路径优先级依次查找
  82. * 直到找到view'文件。
  83. * 1.get exist view file from mutil theme by theme protity.
  84. * 2.get content by yii view compontent function renderFile() ,
  85. */
  86. public function render($view, $params = [])
  87. {
  88. $viewFile = Yii::$service->page->theme->getViewFile($view);
  89. $content = Yii::$app->view->renderFile($viewFile, $params, $this);
  90. return $this->renderContent($content);
  91. }
  92. /**
  93. * @param $view|string
  94. * Get current layoutFile absolute path from mutil theme dir by protity.
  95. * 首先在高级别的模板中找view文件,如果找不到,按照模板路径优先级依次查找
  96. * 直到找到view'文件。
  97. */
  98. public function findLayoutFile($view)
  99. {
  100. $layoutFile = '';
  101. $relativeFile = 'layouts/'.Yii::$service->page->theme->layoutFile;
  102. $absoluteDir = Yii::$service->page->theme->getThemeDirArr();
  103. foreach ($absoluteDir as $dir) {
  104. if ($dir) {
  105. $file = $dir.'/'.$relativeFile;
  106. if (file_exists($file)) {
  107. $layoutFile = $file;
  108. return $layoutFile;
  109. }
  110. }
  111. }
  112. throw new InvalidValueException('layout file is not exist!');
  113. }
  114. }