《电子商城实录------项目目录的结构搭建及其说明2》中方法优化
Framework.class.php代码加入static:
<?php
//核心启动类
class Framework{
public static function run(){
echo "hello,wrold!";
}
//初始化方法
private static function init(){
//定义路径常量
define("DS",DIRECTORY_SEPARATOR);
define("ROOT",getcwd().‘/‘);
define("APP_PATH",ROOT.‘application‘.DS);
define("FRAMEWORK_PATH",ROOT.‘framework‘.DS);
define("PUBLIC_PATH",ROOT.‘public‘.DS);
define("GONFIG_PATH",APP_PATH."config".DS);
define("CONTROLLER_PATH",APP_PATH."controllers".DS);
define("MODEL_PATH",APP_PATH."models".DS);
define("VIEW_PATH",APP_PATH.‘views‘.DS);
define("CORE_PATH",FRAMEWORK_PATH.‘core‘.DS);
define("DB_PATH",FRAMEWORK_PATH.‘databases‘.DS);
define("LIB_PATH",FRAMEWORK_PATH.‘libraries‘.DS);
define("HELPER_PATH",FRAMEWORK_PATH.helpers);
define("UPLOAD_PATH",PUBLIC_PATH.‘uploads‘.DS);
//获取参数p ,c,a index.php?p=admin&c=goods&a=add
define(‘PLATFORM‘, isset($_GET[‘p‘])?$_GET[‘p‘]:"admin");
define(‘CONTROLLER‘,isset($_GET[‘c‘])?$_GET[‘c‘]:"Index");
define(‘ACTION‘,isset($_GET[‘a‘])?$_GET[‘a‘]:"index");
}
//路由方法
private static function dispatch(){
}
//自动加载
private static function autoload(){
}
}
?>
index.php的改进为:
<?php
//入口文件
//引入核心启动类
include ‘framework/core/Framework.class.php‘;
//$app=new Framework();
//$app->run();
Framework::run();
echo getcwd();
?>
我们做项目第一步就是做目录的路径常量
1.做根目录的路径常量:
define("DS",DIRECTORY_SEPARATOR);
define("ROOT",getcwd().‘/‘);
第二步 做application目录
define("APP_PATH",ROOT.‘application‘.DS);
第三步 做framework目录
define("FRAMEWORK_PATH",ROOT.‘framework‘.DS);
第四步 做public路径常量
define("PUBLIC_PATH",ROOT.‘public‘.DS);
第五步 建立config目录
define("GONFIG_PATH",APP_PATH."config".DS);
其他目录以此类推:
我就不一一画图了
define("CONTROLLER_PATH",APP_PATH."controllers".DS);
define("MODEL_PATH",APP_PATH."models".DS);
define("VIEW_PATH",APP_PATH.‘views‘.DS);
define("CORE_PATH",FRAMEWORK_PATH.‘core‘.DS);
define("DB_PATH",FRAMEWORK_PATH.‘databases‘.DS);
define("LIB_PATH",FRAMEWORK_PATH.‘libraries‘.DS);
define("HELPER_PATH",FRAMEWORK_PATH.helpers);
define("UPLOAD_PATH",PUBLIC_PATH.‘uploads‘.DS);
第六步 获取参数p ,c,a
类比百度:
https://www.baidu.com/?tn=98010089_dg&ch=12
后面这种方式
http://localhost:8989/ index.php?p=admin&c=goods&a=add
//获取参数p ,c,a
define(‘PLATFORM‘, isset($_GET[‘p‘])?$_GET[‘p‘]:"admin");
define(‘CONTROLLER‘,isset($_GET[‘c‘])?ucfirst($_GET[‘c‘]):"Index");
define(‘ACTION‘,isset($_GET[‘a‘])?$_GET[‘a‘]:"index");
第七步设置当前控制器和视图
define("CUR_CONTROLLER_PATH",CONTROLLER_PATH.PLATFORM.DS);
define("CUR_VIEW_PATH",VIEW_PATH.PLATFORM.DS);
整体代码如下:
<?php
//核心启动类
class Framework{
public static function run(){
echo "hello,wrold!";
}
//初始化方法
private static function init(){
//定义路径常量
define("DS",DIRECTORY_SEPARATOR);
define("ROOT",getcwd().‘/‘);
define("APP_PATH",ROOT.‘application‘.DS);
define("FRAMEWORK_PATH",ROOT.‘framework‘.DS);
define("PUBLIC_PATH",ROOT.‘public‘.DS);
define("GONFIG_PATH",APP_PATH."config".DS);
define("CONTROLLER_PATH",APP_PATH."controllers".DS);
define("MODEL_PATH",APP_PATH."models".DS);
define("VIEW_PATH",APP_PATH.‘views‘.DS);
define("CORE_PATH",FRAMEWORK_PATH.‘core‘.DS);
define("DB_PATH",FRAMEWORK_PATH.‘databases‘.DS);
define("LIB_PATH",FRAMEWORK_PATH.‘libraries‘.DS);
define("HELPER_PATH",FRAMEWORK_PATH.helpers);
define("UPLOAD_PATH",PUBLIC_PATH.‘uploads‘.DS);
//获取参数p ,c,a
define(‘PLATFORM‘, isset($_GET[‘p‘])?$_GET[‘p‘]:"admin");
define(‘CONTROLLER‘,isset($_GET[‘c‘])?ucfirst($_GET[‘c‘]):"Index");
define(‘ACTION‘,isset($_GET[‘a‘])?$_GET[‘a‘]:"index");
//设置当前控制器和视图
define("CUR_CONTROLLER_PATH",CONTROLLER_PATH.PLATFORM.DS);
define("CUR_VIEW_PATH",VIEW_PATH.PLATFORM.DS);
}
//路由方法
private static function dispatch(){
}
//自动加载
private static function autoload(){
}
}
?>
原文地址:https://www.cnblogs.com/zhouxingzhi/p/10306821.html