protected static function _router($controllers_path, $path) { if ($path && is_string($path)) { // 字符串 myweb/home/index/a/b $path = explode('/', trim($path, '/')); } elseif ($path && is_array($path)) { // 数组 array( 'myweb', 'home', 'index', 'a', 'b' ) } //是否带文件目录 if (CONTROLLERS_USERDIR && $path) { self::$_fileDir = array_shift($path); } self::_fileClassMethod($path); //查找控制器 $class_name = self::$_class . ".php"; $dir_path = '/'; $arr_name = '_controllers'; $rs = self::_findClass($controllers_path, $class_name, $arr_name, $dir_path); if (!$rs) { if (self::$_class !== 'favicon.ico') { Log::write2("controller找不到类:" . self::$_class, 'router'); } return false; } $controll = new self::$_class(); //执行类方法 call_user_func_array(array($controll, self::$_method), self::$_args); }
<?php //根路径 define("ROOT_PATH", str_replace('\\', '/', dirname(__FILE__) . DIRECTORY_SEPARATOR)); //初始化 require_once "include.php"; //路由器 try { $path = isset($_GET['param']) ? $_GET['param'] : ''; Lu::router($path); } catch (Exception $e) { $msg = $e->getMessage(); if ($msg && $msg !== 'exit') { Log::write2($msg, 'myweb'); } }
protected static function _findClass($root_path, $class_name, $arr_name, $dir_path) { ${$arr_name} = self::${$arr_name}; if (!${$arr_name}) { Log::write2("找不到{$arr_name目录}", 'router'); return false; } //如果路径带文件夹 if (self::$_fileDir && $arr_name === '_controllers') { if (!in_array(self::$_fileDir, ${$arr_name})) { Log::write2($arr_name . "目录找不到" . self::$_fileDir, 'router'); self::$_fileDir = null; return false; } } if (${$arr_name}) { foreach (${$arr_name} as $m) { $file_path = $root_path . $m . $dir_path . $class_name; if (is_file($file_path)) { require_once $file_path; return true; } } } return false; }
<?php //注册一个自动加载类函数(在类调用前注册,否则可能会报一个致命错误,而导致程序退出) spl_autoload_register(array("Lu", "findClass")); //加载controller文件夹下的类 Lu::$_controllers = (include ROOT_PATH . 'config/' . "controller.config.php"); //加载model文件夹下的类 Lu::$_models = (include ROOT_PATH . 'config/' . "model.config.php"); //加载libs/plugins里面的类(如果开启了自动按需加载) if (OPEN_AUTO_LOAD_PLUGINS === true) { Lu::$_plugins = array("image", "mailer", "qq", "upload", "weibo", "weixin", "wuliu", "xs"); } //初始化设置cookie LuCookie::initCookie();