定义核心框架类
<?php
class Framework{
//运行方法
public static function run(){
static::init();//初始化
static::autoload();//加载类
static::dispatch();//路由分发
}
/**
* 初始化
*/
private static function init(){
define("DS",DIRECTORY_SEPARATOR); //目录路径分隔符常量
define("ROOT",getcwd().DS); //根目录常量
define("FRAMEWORK_PATH",ROOT.'framework'.DS); //framework目录常量
define("CORE_PATH",FRAMEWORK_PATH.'core'.DS); // ./framework/core目录常量
define("DATABASES_PATH",FRAMEWORK_PATH.'databases'.DS); // ./framework/databases目录常量
define("HELPERS_PATH",FRAMEWORK_PATH.'helpers'.DS); // ./framework/helpers目录常量
define("LIBRARIES",FRAMEWORK_PATH.'libraries'.DS);
define("APP_PATH",ROOT.'application'.DS); //application目录常量
define("CONTROLLER_PATH",APP_PATH.'controllers'.DS); // ./application/controllers目录常量
define("MODEL_PATH",APP_PATH.'models'.DS); // ./application/models目录常量
define("VIEWS_PATH",APP_PATH.'views'.DS); // ./application/views
define("UPLOADS_PATH",ROOT.'public'.DS.'uploads'.DS); // ./public/uploads目录常量
/*根据$_GET获取的平台参数来定义平台常量
* 使用的控制器 需要注意的是一般习惯性上index.php?p=admin&c=index&a=index 需要把c=index的值转换为大写 ucnfrist();
* 控制器中的方法*/
define('PLATFORM',isset($_GET['p'])?$_GET['p']:"admin"); //当前平台常量,默认admin
define('CONTROLLER',isset($_GET['c'])? ucfirst($_GET['c']):"Index"); //当前所使用的控制器,默认index
define('ACTION',isset($_GET['a'])? $_GET['a']:"index"); //当前所使用的控制器中的方法
/*定义控制器目录常量
* 视图目录常量
* */
define("CUR_CONTROLLER_PATH",CONTROLLER_PATH.PLATFORM.DS);
define("CUR_VIEWS_PATH",VIEWS_PATH.PLATFORM.DS);
}
/**
* 路由分发
*/
private static function dispatch(){
/*构建控制器变量名
* 构建控制器方法变量名
* */
$controller_name=CONTROLLER."Controller";
$action_name=ACTION."Action";
/*实例化控制器类
* 调用控制器方法
* */
$controller_obj=new $controller_name();
$controller_obj->$action_name();
}
/**
* 注册自动加载
*/
private static function autoload(){
//注册自动加载的函数
spl_autoload_register('self::load');
}
/**
* 自动加载
*/
private static function load($class_name){
if(substr($class_name, -5)=="Model"){ //如果类名是Model,载入要使用的模型类
include MODEL_PATH."{$class_name}.class.php";
}else if(substr($class_name, -10)=="Controller"){ //如果类名是Controller,载入要使用的控制器类
include CUR_CONTROLLER_PATH."{$class_name}.class.php";
}else{
//待定
}
}
}
定义IndexController控制器
<?php
class IndexController{
/**
* 载入页面视图
*/
public function indexAction(){
include CUR_VIEWS_PATH."index.html";
}
public function dragAction(){
include CUR_VIEWS_PATH.'drag.html';
}
public function mainAction(){
include CUR_VIEWS_PATH.'main.html';
}
public function topAction(){
include CUR_VIEWS_PATH.'top.html';
}
public function menuAction(){
include CUR_VIEWS_PATH.'menu.html';
}
}
定义前端分发器index.php
include_once './framework/core/Framework.class.php';//载入
Framework::run(); //调用run();方法
这里写代码片