Controller.php 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Yzncms [ 御宅男工作室 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2018 http://yzncms.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: 御宅男 <530765310@qq.com>
  10. // +----------------------------------------------------------------------
  11. // +----------------------------------------------------------------------
  12. // | 插件控制器
  13. // +----------------------------------------------------------------------
  14. namespace think\addons;
  15. use app\common\controller\BaseController;
  16. use think\App;
  17. use think\facade\Config;
  18. use think\facade\Event;
  19. use think\facade\Lang;
  20. use think\facade\View;
  21. use think\helper\Str;
  22. /**
  23. * 插件基类控制器.
  24. */
  25. class Controller extends BaseController
  26. {
  27. // 当前插件操作
  28. protected $addon = null;
  29. protected $controller = null;
  30. protected $action = null;
  31. // 当前template
  32. protected $template;
  33. /**
  34. * 架构函数.
  35. */
  36. public function __construct(App $app)
  37. {
  38. //移除HTML标签
  39. app()->request->filter('trim,strip_tags,htmlspecialchars');
  40. // 是否自动转换控制器和操作名
  41. $convert = Config::get('url_convert');
  42. $filter = $convert ? 'strtolower' : 'trim';
  43. // 处理路由参数
  44. $var = $param = app()->request->param();
  45. $addon = isset($var['addon']) ? $var['addon'] : '';
  46. $controller = isset($var['controller']) ? $var['controller'] : '';
  47. $action = isset($var['action']) ? $var['action'] : '';
  48. $this->addon = $addon ? call_user_func($filter, $addon) : '';
  49. $this->controller = $controller ? call_user_func($filter, $controller) : 'index';
  50. $this->action = $action ? call_user_func($filter, $action) : 'index';
  51. // 重置配置
  52. Config::set(['view_path' => ADDON_PATH . $this->addon . DIRECTORY_SEPARATOR . 'view' . DIRECTORY_SEPARATOR], 'view');
  53. // 父类的调用必须放在设置模板路径之后
  54. parent::__construct($app);
  55. }
  56. protected function _initialize()
  57. {
  58. // 渲染配置到视图中
  59. $config = get_addon_config($this->addon);
  60. $this->view->config(['view_path' => ADDON_PATH . $this->addon . DIRECTORY_SEPARATOR . 'view' . DIRECTORY_SEPARATOR]);
  61. $this->view->assign('config', $config);
  62. }
  63. /**
  64. * 加载模板输出.
  65. *
  66. * @param string $template 模板文件名
  67. * @param array $vars 模板输出变量
  68. * @param array $replace 模板替换
  69. * @param array $config 模板参数
  70. *
  71. * @return mixed
  72. */
  73. protected function fetch($template = '', $vars = [], $replace = [], $config = [])
  74. {
  75. $controller = Str::studly($this->controller);
  76. if ('think' == strtolower(Config::get('template.type')) && $controller && 0 !== strpos($template, '/')) {
  77. $depr = Config::get('template.view_depr');
  78. $template = str_replace(['/', ':'], $depr, $template);
  79. if ('' == $template) {
  80. // 如果模板文件名为空 按照默认规则定位
  81. $template = str_replace('.', DIRECTORY_SEPARATOR, $controller) . $depr . $this->action;
  82. } elseif (false === strpos($template, $depr)) {
  83. $template = str_replace('.', DIRECTORY_SEPARATOR, $controller) . $depr . $template;
  84. }
  85. }
  86. return View::fetch($template, $vars);
  87. }
  88. }