|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139 |
- <?php
- /**
- * 框架类
- * ============================================================================
- * * 版权所有 蜘蛛出行 * *
- * 网站地址: http://www.zhizhuchuxing.com
- * ----------------------------------------------------------------------------
- * 这不是一个自由软件!您只能在不用于商业目的的前提下对程序代码进行修改和
- * 使用;不允许对程序代码以任何形式任何目的的再发布。
- * ============================================================================
- * Author By: 倪宗锋
- * PhpStorm ApplicationController.php
- * Create By 2016/11/4 15:02 $
- */
-
-
- namespace Util\Controller;
-
- use Util\Util\ApcCache;
- use Util\Util\Util;
-
- class ApplicationController
- {
- public $class = '';
- public $action = '';
- public $viewUrl = '';
-
- public function __construct()
- {
- $this->setSession();
- $this->setControllerAndAction();
- }
-
- /**
- * Function Description:执行程序
- * Function Name: run
- *
- *
- * @author 倪宗锋
- */
- public function run()
- {
- $controller = $this->class;//控制器
- $action = $this->action;//方法
- /**==========执行控制器和方法==========**/
- if (class_exists($controller)) {//如果控制器存在
- $application = new $controller;
- if (method_exists($application, $action)) {//如果方法存在
- $result = $application->$action();
- } else {
- $result = Util::getErrUrlReturn();
- }
- } else {
- $result = Util::getErrUrlReturn();
- }
- echo $result;
- }
-
- /**
- * Function Description:设置路由的控制器class和action
- * Function Name: setControllerAndAction
- *
- * @return string
- *
- * @author 倪宗锋
- */
- public function setControllerAndAction()
- {
- /**==========从路由中获取控制器和方法名称==========**/
- $routesConfig = $this->getRoutesConfig();
- $requestUrl = parse_url($_SERVER['REQUEST_URI']);
- $url = $requestUrl['path'];
- $urlArr = explode('/', $url);
- $route = '/';
- //通过循环匹配符合条件的路由 匹配成功循环不终止
- foreach ($urlArr as $key => $urlVal) {
- $route = $route . '/' . $urlVal;
- $route = str_replace('//', '/', $route);
- if (empty($routesConfig[$route]) == false) {//如果路由在配置文件中则使用该配置
- $routeArr = $routesConfig[$route];
- $de_action = empty($urlArr[$key + 1]) ? $routeArr['de_action'] : $urlArr[$key + 1];
- $this->class = $routeArr['controller'];//读取该配置的控制器
- $this->action = $de_action . 'Action';
- }
- }
- return '';
- }
-
- /**
- * Function Description:获取模块的配置信息
- * Function Name: getModelConfig
- *
- * @return array
- *
- * @author 倪宗锋
- */
- public function getRoutesConfig()
- {
- $key = 'frameWorkRoutesConfig';
- $routesConfig = ApcCache::get($key);//获取路由配置并合并
- if ($routesConfig == false) {
- $routesConfig = array();
- $applicationConfig = require ROOT_PATH . '/config/application.config.php';
- if (empty($applicationConfig['modules']) == false) {
- foreach ($applicationConfig['modules'] as $val) {//循环读取控制器文件合并
- $filename = ROOT_PATH . "/$val/Config/model.config.php";
- if (is_file($filename)) {
- $config = require $filename;
- $routesConfig = $routesConfig + $config;
- }
- }
- ApcCache::set($key, $routesConfig, 3600);
- }
- }
- return $routesConfig;
- }
-
- /**
- * Function Description:设置项目session
- * Function Name: setSession
- *
- *
- * @author 倪宗锋
- */
- private function setSession()
- {
- $config = Util::getSiteConfig();
- session_start();
- setcookie(session_name(), session_id(), time() + $config['session_life'], "/");
- }
-
- /**
- * 项目关闭
- */
- protected function __destruct()
- {
- session_commit();//session数据提交
- }
- }
|