Frontend.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. namespace app\common\controller;
  3. use think\Config;
  4. use think\Controller;
  5. use think\Hook;
  6. use think\Lang;
  7. class Frontend extends Controller
  8. {
  9. /**
  10. * 布局模板
  11. * @var string
  12. */
  13. protected $layout = '';
  14. public function _initialize()
  15. {
  16. //移除HTML标签
  17. $this->request->filter('strip_tags');
  18. $modulename = $this->request->module();
  19. $controllername = strtolower($this->request->controller());
  20. $actionname = strtolower($this->request->action());
  21. // 如果有使用模板布局
  22. if ($this->layout)
  23. {
  24. $this->view->engine->layout('layout/' . $this->layout);
  25. }
  26. // 语言检测
  27. $lang = strip_tags(Lang::detect());
  28. $site = Config::get("site");
  29. $upload = \app\common\model\Config::upload();
  30. // 上传信息配置后
  31. Hook::listen("upload_config_init", $upload);
  32. // 配置信息
  33. $config = [
  34. 'site' => array_intersect_key($site, array_flip(['name', 'cdnurl', 'version', 'timezone', 'languages'])),
  35. 'upload' => $upload,
  36. 'modulename' => $modulename,
  37. 'controllername' => $controllername,
  38. 'actionname' => $actionname,
  39. 'jsname' => 'frontend/' . str_replace('.', '/', $controllername),
  40. 'moduleurl' => rtrim(url("/{$modulename}", '', false), '/'),
  41. 'language' => $lang
  42. ];
  43. Config::set('upload', array_merge(Config::get('upload'), $upload));
  44. // 配置信息后
  45. Hook::listen("config_init", $config);
  46. $this->loadlang($controllername);
  47. $this->assign('site', $site);
  48. $this->assign('config', $config);
  49. }
  50. /**
  51. * 加载语言文件
  52. * @param string $name
  53. */
  54. protected function loadlang($name)
  55. {
  56. Lang::load(APP_PATH . $this->request->module() . '/lang/' . Lang::detect() . '/' . str_replace('.', '/', $name) . '.php');
  57. }
  58. /**
  59. * 渲染配置信息
  60. * @param mixed $name 键名或数组
  61. * @param mixed $value 值
  62. */
  63. protected function assignconfig($name, $value = '')
  64. {
  65. $this->view->config = array_merge($this->view->config ? $this->view->config : [], is_array($name) ? $name : [$name => $value]);
  66. }
  67. }