ComponentHelper.php 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. <?php
  2. namespace common\helpers;
  3. use Yii;
  4. /**
  5. * 组件助手类
  6. * @Author bing
  7. * @DateTime 2021-08-16 18:13:37
  8. * @copyright: Copyright (c) 2020 广东七件事集团
  9. */
  10. class ComponentHelper{
  11. public static $loadedComponents = [];
  12. /**
  13. * 载入后端组件视图
  14. * @Author bing
  15. * @DateTime 2021-08-16 18:53:18
  16. * @copyright: Copyright (c) 2020 广东七件事集团
  17. * @param [type] $component
  18. * @param [type] $componentPath
  19. * @param boolean $onceLoad
  20. * @return void
  21. */
  22. public static function loadComponentView($component, $componentPath = null, $params = [], $onceLoad = true,$suffix='php'){
  23. if (!$componentPath) {
  24. $componentPath = \Yii::$app->viewPath . '/components/common';
  25. }
  26. $component_file = "{$componentPath}/{$component}.$suffix";
  27. if (isset(self::$loadedComponents[$component_file]) && $onceLoad) {
  28. //此组件已经载入过了
  29. return;
  30. }
  31. self::$loadedComponents[$component_file] = true;
  32. //输出页面
  33. echo Yii::$app->getView()->renderFile($component_file, $params) . "\n";
  34. }
  35. }