123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168 |
- <?php namespace App\Http\Controllers;
- // 队列服务
- use Illuminate\Foundation\Bus\DispatchesJobs;
- // 验证请求
- use Illuminate\Foundation\Validation\ValidatesRequests;
- // 基础控制器
- use Illuminate\Routing\Controller as BaseController;
- // 配置模型
- use App\Models\Config;
- // 视图工厂
- use Illuminate\Contracts\View\Factory as ViewFactory;
- /**
- * 控制器总控
- *
- */
- class Controller extends BaseController
- {
- use DispatchesJobs, ValidatesRequests;
- // 模板变量
- protected $bladeData = [];
- /**
- * 构造方法,初始化
- *
- * */
- public function __construct(){
- // 初始化
- if( method_exists($this, '_initialize') ) $this->_initialize();
- // 初始化
- if( method_exists($this, '__init') ) $this->__init();
- // 读取配置
- $config = $this->loadConfig();
- // 动态配置
- config($config);
- }
- /* 初始化 */
- protected function _initialize(){
-
- }
- /* 初始化 */
- protected function __init(){
-
- }
- /* 读取配置 */
- protected function loadConfig(){
- // 返回配置
- return (new Config)->getConfigList();
- }
- /**
- * 操作错误跳转的快捷方法
- * @param Mixed $msg 提示信息
- * @param String $url 跳转的URL地址
- * @param Int $wait 跳转等待时间
- *
- * @return Void
- */
- protected function error($msg = '', $url = null, $wait = 3)
- {
- if ( is_null($url) ) {
- $url = request()->ajax() ? '' : 'javascript:history.back(-1);';
- } elseif ('' !== $url) {
- $url = (strpos($url, '://') || 0 === strpos($url, '/')) ? $url : url($url);
- }
- // 返回结果
- return view('error',['msg'=>$msg,'url'=>$url,'wait'=>$wait]);
- }
- /**
- * 获取视图内容
- *
- * @param string|null $view
- * @param \Illuminate\Contracts\Support\Arrayable|array $data
- * @param array $mergeData
- * @return \Illuminate\Contracts\View\View|\Illuminate\Contracts\View\Factory
- */
- protected function fetch($view = null, $data = [], $mergeData = [])
- {
- // 如果没有指定路径使用对应模板路径
- if (!$view) $view = $this->getBladePath();
- // 如果有数据
- if( $data ) $this->bladeData = array_merge($this->bladeData,$data);
- // 获取所有模板变量
- $data = $this->bladeData;
- // 获取工厂类
- $factory = app(ViewFactory::class);
- // 创建视图内容并返回
- return $factory->make($view, $data, $mergeData);
- }
- /**
- * 模板变量赋值
- * @access protected
- * @param mixed $name 要显示的模板变量
- * @param mixed $value 变量的值
- * @return $this
- */
- protected function assign( $name, $value = '')
- {
- // 如果是数组
- if( is_array($name) ){
- // 合并数据
- $this->bladeData = array_merge($this->bladeData,$name);
- }else{
- // 否则增加或者更新
- $this->bladeData[$name] = $value;
- }
- // 返回结果
- return $this->bladeData;
- }
- /**
- * 获取模板文件路径
- *
- */
- protected function getBladePath()
- {
- // 获取当前请求的控制器命名空间以及方法
- $actionName = request()->route()->getActionName();
- // 切割成控制器路径和方法
- list($class, $method) = explode('@',$actionName);
- /* 获取模块名 */
- // 以反斜杠切割成数组
- $modules = implode('\\', array_slice(explode('\\', $class), 0, -1));
- // 去除斜杠
- $modules = trim($modules,'\\');
- // 替换掉数据
- $modules = str_replace('App\\Http\\Controllers\\','',$modules);
- // 模块名
- $modules = str_replace('\\','/',$modules);
- // 控制器名称
- $controller = str_replace('Controller','',substr(strrchr($class, '\\'), 1));
- // 转下划线格式
- $controller = $this->humpToSnake($controller);
- // 模板路径
- $bladePath = implode('.',[$modules,$controller,$method]);
- // 全转小写
- $bladePath = strtolower($bladePath);
- // 返回结果
- return $bladePath;
- }
- /**
- * 驼峰转下划线
- *
- */
- protected function humpToSnake($camelCaps,$separator='_')
- {
- return strtolower(preg_replace('/([a-z])([A-Z])/', "$1" . $separator . "$2", $camelCaps));
- }
- /**
- * 驼峰转下划线
- *
- */
- protected function snakeToHump($camelCaps,$separator='_')
- {
- return strtolower(preg_replace('/([a-z])([A-Z])/', "$1" . $separator . "$2", $camelCaps));
- }
- }
|