Controller.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. <?php namespace App\Http\Controllers;
  2. // 队列服务
  3. use Illuminate\Foundation\Bus\DispatchesJobs;
  4. // 验证请求
  5. use Illuminate\Foundation\Validation\ValidatesRequests;
  6. // 基础控制器
  7. use Illuminate\Routing\Controller as BaseController;
  8. // 配置模型
  9. use App\Models\Config;
  10. // 视图工厂
  11. use Illuminate\Contracts\View\Factory as ViewFactory;
  12. /**
  13. * 控制器总控
  14. *
  15. */
  16. class Controller extends BaseController
  17. {
  18. use DispatchesJobs, ValidatesRequests;
  19. // 模板变量
  20. protected $bladeData = [];
  21. /**
  22. * 构造方法,初始化
  23. *
  24. * */
  25. public function __construct(){
  26. // 初始化
  27. if( method_exists($this, '_initialize') ) $this->_initialize();
  28. // 初始化
  29. if( method_exists($this, '__init') ) $this->__init();
  30. // 读取配置
  31. $config = $this->loadConfig();
  32. // 动态配置
  33. config($config);
  34. }
  35. /* 初始化 */
  36. protected function _initialize(){
  37. }
  38. /* 初始化 */
  39. protected function __init(){
  40. }
  41. /* 读取配置 */
  42. protected function loadConfig(){
  43. // 返回配置
  44. return (new Config)->getConfigList();
  45. }
  46. /**
  47. * 操作错误跳转的快捷方法
  48. * @param Mixed $msg 提示信息
  49. * @param String $url 跳转的URL地址
  50. * @param Int $wait 跳转等待时间
  51. *
  52. * @return Void
  53. */
  54. protected function error($msg = '', $url = null, $wait = 3)
  55. {
  56. if ( is_null($url) ) {
  57. $url = request()->ajax() ? '' : 'javascript:history.back(-1);';
  58. } elseif ('' !== $url) {
  59. $url = (strpos($url, '://') || 0 === strpos($url, '/')) ? $url : url($url);
  60. }
  61. // 返回结果
  62. return view('error',['msg'=>$msg,'url'=>$url,'wait'=>$wait]);
  63. }
  64. /**
  65. * 获取视图内容
  66. *
  67. * @param string|null $view
  68. * @param \Illuminate\Contracts\Support\Arrayable|array $data
  69. * @param array $mergeData
  70. * @return \Illuminate\Contracts\View\View|\Illuminate\Contracts\View\Factory
  71. */
  72. protected function fetch($view = null, $data = [], $mergeData = [])
  73. {
  74. // 如果没有指定路径使用对应模板路径
  75. if (!$view) $view = $this->getBladePath();
  76. // 如果有数据
  77. if( $data ) $this->bladeData = array_merge($this->bladeData,$data);
  78. // 获取所有模板变量
  79. $data = $this->bladeData;
  80. // 获取工厂类
  81. $factory = app(ViewFactory::class);
  82. // 创建视图内容并返回
  83. return $factory->make($view, $data, $mergeData);
  84. }
  85. /**
  86. * 模板变量赋值
  87. * @access protected
  88. * @param mixed $name 要显示的模板变量
  89. * @param mixed $value 变量的值
  90. * @return $this
  91. */
  92. protected function assign( $name, $value = '')
  93. {
  94. // 如果是数组
  95. if( is_array($name) ){
  96. // 合并数据
  97. $this->bladeData = array_merge($this->bladeData,$name);
  98. }else{
  99. // 否则增加或者更新
  100. $this->bladeData[$name] = $value;
  101. }
  102. // 返回结果
  103. return $this->bladeData;
  104. }
  105. /**
  106. * 获取模板文件路径
  107. *
  108. */
  109. protected function getBladePath()
  110. {
  111. // 获取当前请求的控制器命名空间以及方法
  112. $actionName = request()->route()->getActionName();
  113. // 切割成控制器路径和方法
  114. list($class, $method) = explode('@',$actionName);
  115. /* 获取模块名 */
  116. // 以反斜杠切割成数组
  117. $modules = implode('\\', array_slice(explode('\\', $class), 0, -1));
  118. // 去除斜杠
  119. $modules = trim($modules,'\\');
  120. // 替换掉数据
  121. $modules = str_replace('App\\Http\\Controllers\\','',$modules);
  122. // 模块名
  123. $modules = str_replace('\\','/',$modules);
  124. // 控制器名称
  125. $controller = str_replace('Controller','',substr(strrchr($class, '\\'), 1));
  126. // 转下划线格式
  127. $controller = $this->humpToSnake($controller);
  128. // 模板路径
  129. $bladePath = implode('.',[$modules,$controller,$method]);
  130. // 全转小写
  131. $bladePath = strtolower($bladePath);
  132. // 返回结果
  133. return $bladePath;
  134. }
  135. /**
  136. * 驼峰转下划线
  137. *
  138. */
  139. protected function humpToSnake($camelCaps,$separator='_')
  140. {
  141. return strtolower(preg_replace('/([a-z])([A-Z])/', "$1" . $separator . "$2", $camelCaps));
  142. }
  143. /**
  144. * 驼峰转下划线
  145. *
  146. */
  147. protected function snakeToHump($camelCaps,$separator='_')
  148. {
  149. return strtolower(preg_replace('/([a-z])([A-Z])/', "$1" . $separator . "$2", $camelCaps));
  150. }
  151. }