CView.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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 fec\helpers;
  10. use Yii;
  11. use yii\base\View;
  12. use yii\base\InvalidConfigException;
  13. /**
  14. * @author Terry Zhao <2358269014@qq.com>
  15. * @since 1.0
  16. */
  17. class CView
  18. {
  19. # 功能块:
  20. # 本功能的作用是通过一个类文件,和一个view 文件,生成一个html块,而且在这个html中还可以嵌套其他的块
  21. # 这样设计的好处:譬如在前端,我们在很多url中都会有一些公用的侧栏块,我们很希望我们的块可以通过配置的方式很容易的加入到侧栏
  22. # 譬如电商网站侧栏的:客户的浏览记录,我们在多个页面都想加入这个功能,我们就可以很方便的做加入。
  23. # 默认的方法,功能块的提供数据的默认方法。可以在配置中配置method方法自定义。
  24. const DATA_METHOD = 'getLastData';
  25. /* 你可以在view 文件中通过下面的方式使用
  26. <?php
  27. use fec\helpers\CView;
  28. $config = [
  29. # 必填
  30. 'class' => 'fec\block\TestMenu',
  31. 'view' => '@fec/views/testmenu/index.php',
  32. # 下面为选填
  33. 'method'=> 'getLastData',
  34. 'terry1'=> 'My1',
  35. 'terry2'=> 'My2',
  36. ];
  37. echo CView::getChildHtml($config)
  38. ?>
  39. */
  40. public static function getChildHtml($config)
  41. {
  42. if( !isset($config['view']) || empty($config['view'])
  43. ){
  44. throw new InvalidConfigException('view and class must exist in array config!');
  45. }
  46. if( !isset($config['class']) || empty($config['class']))
  47. return Yii::$app->view->render($config['view'], []);
  48. $method = self::DATA_METHOD;
  49. if(isset($config['method']) && !empty($config['method'])){
  50. $method = $config['method'];
  51. unset($config['method']);
  52. }
  53. $view = $config['view'];
  54. unset($config['view']);
  55. $ob = Yii::createObject($config);
  56. $params = $ob->$method();
  57. return Yii::$app->view->render($view, $params);
  58. }
  59. # 通过配置
  60. /*
  61. 1.add config param to modules params or application params.
  62. params.php
  63. [
  64. 'params' => [
  65. 'block' =>[
  66. 'menu' =>[
  67. # 必填
  68. 'class' => 'fec\block\TestMenu',
  69. 'view' => '@fec/views/testmenu/index.php',
  70. # 下面为选填
  71. 'method'=> 'getLastData',
  72. 'terry1'=> 'My1',
  73. 'terry2'=> 'My2',
  74. ],
  75. ]
  76. ]
  77. ]
  78. 2.
  79. use fec\helpers\CView;
  80. CView::getConfigChildHtml('menu');
  81. */
  82. public static function getConfigChildHtml($configKey){
  83. $config = [];
  84. # get config from module param
  85. if($module = Yii::$app->controller->module){
  86. $module_config = CModule::param("block");
  87. if(isset($module_config[$configKey])){
  88. $config = $module_config[$configKey];
  89. }
  90. }
  91. # if module config param is empty or not exist,
  92. # get config from application
  93. if(empty($config)){
  94. $app_config = CConfig::param("block");
  95. if(isset($app_config[$configKey])){
  96. $config = $app_config[$configKey];
  97. }
  98. }
  99. if(!isset($config['view']) || empty($config['view'])
  100. ){
  101. throw new InvalidConfigException('view and class must exist in array config!');
  102. }else{
  103. return self::getChildHtml($config);
  104. }
  105. }
  106. }