Frontend.php 2.1 KB

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