|
<?php
namespace daswork;
use \daswork\lib\Route;
use \daswork\lib\Config;
class App{
public static $classMap = array();#用于判断类是否存在,节约性能
/**
* 运行控制器和方法
* @throws \Exception
*/
public static function run(){
self::init();
$route = new Route();
$model = $route->model;
$ctrlClass = '\\app\\' . $model . '\\' . "controller" . '\\'. $route->ctrl;
$action = $route->action;
$ctrl = new $ctrlClass();
$ctrl->$action();
}
public static function init()
{
Config::load();
}
}
// /**
// * 执行应用程序
// * @access public
// * @param Request $request Request对象
// * @return Response
// * @throws Exception
// */
// public static function run(Request $request = null)
// {
// is_null($request) && $request = Request::instance();
// try {
// $config = self::initCommon();
// if (defined('BIND_MODULE')) {
// // 模块/控制器绑定
// BIND_MODULE && Route::bind(BIND_MODULE);
// } elseif ($config['auto_bind_module']) {
// // 入口自动绑定
// $name = pathinfo($request->baseFile(), PATHINFO_FILENAME);
// if ($name && 'index' != $name && is_dir(APP_PATH . $name)) {
// Route::bind($name);
// }
// }
// $request->filter($config['default_filter']);
// // 默认语言
// Lang::range($config['default_lang']);
// if ($config['lang_switch_on']) {
// // 开启多语言机制 检测当前语言
// Lang::detect();
// }
// $request->langset(Lang::range());
// // 加载系统语言包
// Lang::load([
// THINK_PATH . 'lang' . DS . $request->langset() . EXT,
// APP_PATH . 'lang' . DS . $request->langset() . EXT,
// ]);
// // 获取应用调度信息
// $dispatch = self::$dispatch;
// if (empty($dispatch)) {
// // 进行URL路由检测
// $dispatch = self::routeCheck($request, $config);
// }
// // 记录当前调度信息
// $request->dispatch($dispatch);
// // 记录路由和请求信息
// if (self::$debug) {
// Log::record('[ ROUTE ] ' . var_export($dispatch, true), 'info');
// Log::record('[ HEADER ] ' . var_export($request->header(), true), 'info');
// Log::record('[ PARAM ] ' . var_export($request->param(), true), 'info');
// }
// // 监听app_begin
// Hook::listen('app_begin', $dispatch);
// // 请求缓存检查
// $request->cache($config['request_cache'], $config['request_cache_expire'], $config['request_cache_except']);
// $data = self::exec($dispatch, $config);
// } catch (HttpResponseException $exception) {
// $data = $exception->getResponse();
// }
// // 清空类的实例化
// Loader::clearInstance();
// // 输出数据到客户端
// if ($data instanceof Response) {
// $response = $data;
// } elseif (!is_null($data)) {
// // 默认自动识别响应输出类型
// $isAjax = $request->isAjax();
// $type = $isAjax ? Config::get('default_ajax_return') : Config::get('default_return_type');
// $response = Response::create($data, $type);
// } else {
// $response = Response::create();
// }
// // 监听app_end
// Hook::listen('app_end', $response);
// return $response;
// }
// <?php
// namespace daswork;
// class Daswork{
// static $config;
// static function run(){
// // self::init_path();
// // self::init_autoload(); //加载文件
// self::config();
// self::dispatch();
// }
// // 加载配置文件
// static function config(){
// self::$config = require(APP_PATH.'/config.php');
// }
// // 获取当前url
// static function get_url()
// {
// $default_m = self::$config['conf_url']['m'];
// $default_c = self::$config['conf_url']['c'];
// $default_a = self::$config['conf_url']['a'];
// $request_url =strtolower( $_SERVER['REQUEST_URI'] );
// $url = array();
// $real_url = array();
// $array_url=explode('/',$request_url);
// foreach($array_url as $v){
// if($v != ''){
// $url[] = $v;
// }
// }
// // var_dump($url);exit;
// $url_count = count($url);
// switch ($url_count) {
// case 0:
// $real_url['m'] = $default_m;
// $real_url['c'] = $default_c;
// $real_url['a'] = $default_a;
// break;
// case 1:
// $real_url['m'] = $url[0];
// $real_url['c'] = $default_c;
// $real_url['a'] = $default_a;
// break;
// case 2:
// $real_url['m'] = $url[0];
// $real_url['c'] = $url[1];
// $real_url['a'] = $default_a;
// break;
// default:
// $real_url['m'] = $url[0];
// $real_url['c'] = $url[1];
// $real_url['a'] = $url[2];
// break;
// }
// // var_dump($real_url);
// return $real_url;
// }
// //加载类并实例化,调用指定方法
// static function dispatch(){
// $real_url =self::get_url();
// define("MODULE",$real_url['m']);
// define("CONTROLLER",ucwords($real_url['c']));
// define("ACTION",$real_url['a']);
// require_once(APP_PATH.'/'.MODULE.'/controller/'.CONTROLLER.'.php');
// //需要加上命名空间
// $classname = '\app\\'.MODULE.'\controller\\'.CONTROLLER;
// $a = ACTION;
// $controller = new $classname; //控制器
// return $controller->$a(); // 调用指定方法
// }
// }
|