You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

139 lines
4.3 KiB

  1. <?php
  2. /**
  3. * 框架类
  4. * ============================================================================
  5. * * 版权所有 蜘蛛出行 * *
  6. * 网站地址: http://www.zhizhuchuxing.com
  7. * ----------------------------------------------------------------------------
  8. * 这不是一个自由软件!您只能在不用于商业目的的前提下对程序代码进行修改和
  9. * 使用;不允许对程序代码以任何形式任何目的的再发布。
  10. * ============================================================================
  11. * Author By: 倪宗锋
  12. * PhpStorm ApplicationController.php
  13. * Create By 2016/11/4 15:02 $
  14. */
  15. namespace Util\Controller;
  16. use Util\Util\ApcCache;
  17. use Util\Util\Util;
  18. class ApplicationController
  19. {
  20. public $class = '';
  21. public $action = '';
  22. public $viewUrl = '';
  23. public function __construct()
  24. {
  25. $this->setSession();
  26. $this->setControllerAndAction();
  27. }
  28. /**
  29. * Function Description:执行程序
  30. * Function Name: run
  31. *
  32. *
  33. * @author 倪宗锋
  34. */
  35. public function run()
  36. {
  37. $controller = $this->class;//控制器
  38. $action = $this->action;//方法
  39. /**==========执行控制器和方法==========**/
  40. if (class_exists($controller)) {//如果控制器存在
  41. $application = new $controller;
  42. if (method_exists($application, $action)) {//如果方法存在
  43. $result = $application->$action();
  44. } else {
  45. $result = Util::getErrUrlReturn();
  46. }
  47. } else {
  48. $result = Util::getErrUrlReturn();
  49. }
  50. echo $result;
  51. }
  52. /**
  53. * Function Description:设置路由的控制器class和action
  54. * Function Name: setControllerAndAction
  55. *
  56. * @return string
  57. *
  58. * @author 倪宗锋
  59. */
  60. public function setControllerAndAction()
  61. {
  62. /**==========从路由中获取控制器和方法名称==========**/
  63. $routesConfig = $this->getRoutesConfig();
  64. $requestUrl = parse_url($_SERVER['REQUEST_URI']);
  65. $url = $requestUrl['path'];
  66. $urlArr = explode('/', $url);
  67. $route = '/';
  68. //通过循环匹配符合条件的路由 匹配成功循环不终止
  69. foreach ($urlArr as $key => $urlVal) {
  70. $route = $route . '/' . $urlVal;
  71. $route = str_replace('//', '/', $route);
  72. if (empty($routesConfig[$route]) == false) {//如果路由在配置文件中则使用该配置
  73. $routeArr = $routesConfig[$route];
  74. $de_action = empty($urlArr[$key + 1]) ? $routeArr['de_action'] : $urlArr[$key + 1];
  75. $this->class = $routeArr['controller'];//读取该配置的控制器
  76. $this->action = $de_action . 'Action';
  77. }
  78. }
  79. return '';
  80. }
  81. /**
  82. * Function Description:获取模块的配置信息
  83. * Function Name: getModelConfig
  84. *
  85. * @return array
  86. *
  87. * @author 倪宗锋
  88. */
  89. public function getRoutesConfig()
  90. {
  91. $key = 'frameWorkRoutesConfig';
  92. $routesConfig = ApcCache::get($key);//获取路由配置并合并
  93. if ($routesConfig == false) {
  94. $routesConfig = array();
  95. $applicationConfig = require ROOT_PATH . '/config/application.config.php';
  96. if (empty($applicationConfig['modules']) == false) {
  97. foreach ($applicationConfig['modules'] as $val) {//循环读取控制器文件合并
  98. $filename = ROOT_PATH . "/$val/Config/model.config.php";
  99. if (is_file($filename)) {
  100. $config = require $filename;
  101. $routesConfig = $routesConfig + $config;
  102. }
  103. }
  104. ApcCache::set($key, $routesConfig, 3600);
  105. }
  106. }
  107. return $routesConfig;
  108. }
  109. /**
  110. * Function Description:设置项目session
  111. * Function Name: setSession
  112. *
  113. *
  114. * @author 倪宗锋
  115. */
  116. private function setSession()
  117. {
  118. $config = Util::getSiteConfig();
  119. session_start();
  120. setcookie(session_name(), session_id(), time() + $config['session_life'], "/");
  121. }
  122. /**
  123. * 项目关闭
  124. */
  125. protected function __destruct()
  126. {
  127. session_commit();//session数据提交
  128. }
  129. }