Frontend.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <?php
  2. namespace app\common\controller;
  3. use app\common\library\Auth;
  4. use app\common\model\Configvalue;
  5. use think\Config;
  6. use think\Controller;
  7. use think\Lang;
  8. class Frontend extends Controller
  9. {
  10. /**
  11. *
  12. * @var Auth
  13. */
  14. protected $user = null;
  15. /**
  16. * 布局模板
  17. * @var string
  18. */
  19. protected $layout = '';
  20. public function _initialize()
  21. {
  22. $modulename = $this->request->module();
  23. $controllername = strtolower($this->request->controller());
  24. $actionname = strtolower($this->request->action());
  25. $path = '/' . $modulename . '/' . str_replace('.', '/', $controllername) . '/' . $actionname;
  26. $this->user = Auth::instance();
  27. // 设置当前请求的URI
  28. $this->user->setRequestUri($path);
  29. // 检测当前是否登录并进行初始化
  30. $this->user->init();
  31. // 将auth对象渲染至视图
  32. $this->view->assign("user", $this->user);
  33. // 如果有使用模板布局
  34. if ($this->layout)
  35. {
  36. $this->view->engine->layout('layout/' . $this->layout);
  37. }
  38. // 语言检测
  39. $lang = Lang::detect();
  40. $site = Config::get("site");
  41. // 配置信息
  42. $config = [
  43. 'site' => array_intersect_key($site, array_flip(['name', 'cdnurl', 'version', 'timezone', 'languages'])),
  44. 'upload' => Configvalue::upload(),
  45. 'modulename' => $modulename,
  46. 'controllername' => $controllername,
  47. 'actionname' => $actionname,
  48. 'jsname' => 'frontend/' . str_replace('.', '/', $controllername),
  49. 'moduleurl' => url("/{$modulename}", '', false),
  50. 'language' => $lang
  51. ];
  52. $this->loadlang($controllername);
  53. $this->assign('site', $site);
  54. $this->assign('config', $config);
  55. }
  56. /**
  57. * 加载语言文件
  58. * @param string $name
  59. */
  60. protected function loadlang($name)
  61. {
  62. Lang::load(APP_PATH . $this->request->module() . '/lang/' . Lang::detect() . '/' . str_replace('.', '/', $name) . '.php');
  63. }
  64. }