开发的项目目录结构
入口文件为index.php所在位置应该为项目更目录下
编程代码为:
<?php
/**
* 入口文件
* 1.定义常量
* 2.加载函数库
* 3.启动框架
*/
/**
* 定义常量
*/
//当前框架所在的根目录
define('ZL',realpath('./'));
//定义核心文件所在的根目录
define('CORE',ZL.'/core');
//定义项目目录
define('APP',ZL.'/app');
define('MODULE','app');
define('DEBUG',true);
if(DEBUG)
{
ini_set('display_error','On');
}
else
{
ini_set('display_error','Off');
}
/**
* 加载函数
*/
include CORE.'/common/function.php';
include CORE.'/ZL.php';
/**
* 启动框架
*/
spl_autoload_register('\core\zl::load');//当加载的类不存在时加载zl下的load方法
\core\zl::run();
框架的核心文件都在./core目录下
基类文件为根目录下的zl.php文件
<?php
namespace core;
class zl
{
public static $classMap = array();
public $assign;
static public function run()
{
$route = new \core\lib\route();
$ctrlClass = $route->ctrl;
$action = $route->action;
$ctrlfile = APP.'/ctrl/'.$ctrlClass.'Ctrl.php';
$ctrlClass = '\\'.MODULE.'\ctrl\\'.$ctrlClass.'Ctrl';
if(is_file($ctrlfile))
{
include $ctrlfile;
$ctrl = new $ctrlClass;
$ctrl->$action();
}
else
{
throw new \Exception('找不到控制器'.$ctrlClass);
}
}
static public function load($class)
{
//自动加载类
//加载路由(new\core\route();)
//$class='\core\route';
//ZL.'/core/route.php';
// p($class);
// p(ZL.$class.'.php');
if(isset($classMap[$class]))
{
return true;
}
else
{
$class=str_replace('\\', '/', $class);
$file=ZL.'/'.$class.'.php';
if(is_file($file))
{
include $file;
self::$classMap[$class] = $class;
}
else
{
return false;
}
}
}
public function assign($name,$value)
{
//p($value);
$this->assign[$name] = $value;
}
public function display($file)
{
$file = APP.'/view/'.$file;
//p($file);
if(is_file($file))
{
//p($file);
extract($this->assign);
include $file;
}
}
}
?>
框架的正常运行离不开路由的设定所以任何框架都需要有路由文件。我的路由文件在./core/lib/route.php文件中
<?php
namespace core\lib;
class route
{
public $ctrl;
public $action;
public function __construct()
{
/**
* 网站访问格式xxx.com/index/index
* 1.隐藏index.php
* 2.获取URL 参数部分
* 3.返回对应控制器和方法
*/
if(isset($_SERVER['REQUEST_URI'])&&$_SERVER['REQUEST_URI']!='/')
{
$path =$_SERVER['REQUEST_URI'];
$patharr=explode('/',trim($path,'/'));
if(isset($patharr[1]))
{
$this->ctrl = $patharr[1];
unset($patharr[1]);
}
if(isset($patharr[2]))
{
$this->action = $patharr[2];
unset($patharr[2]);
}
else
{
$this->action = 'index';
}
//url多余部分转换成GET值
$count = count($patharr)+2;
//p($count);
$i=3;
while($i < $count)
{
if(isset($patharr[$i + 1]))
{
$_GET[$patharr[$i]] = $patharr[$i + 1];
}
$i = $i + 2;
}
}
else
{
$this->ctrl='index';
$this->action='index';
}
}
}
?>
对于上述代码中的第一步隐藏index.php文件何以使用url重写来实现文件为根目录下的.htaccess文件
函数库:
所在位置:./core/common/function.php
<?php
function p($var)
{
if(is_bool($var))
{
var_dump($var);
}
else if(is_null($var))
{
var_dump(NULL);
}
else
{
echo "<pre style='position:relative;z-index:1000;padding:10px;
border-radius:5px;background:#CCCCCC;border:1px splid:#aaa;font-size:14px;
line-height:18px;opacity:0.9'>".print_r($var,true)."</pre>";
}
}
?>
<IfModule mod_rewrite.c>
RewriteEngine Off
# 确保请求路径不是一个文件名或目录
RewriteCond %{REQUEST_FILENAME} !-f
#RewriteCond %{REQUEST_FILENAME} !-d
# 重定向所有请求到 index.php?url=PATHNAME
RewriteRule ^(.*)$ index.php?/$1 [QSA,PT,L]
</IfModule>
特别声明,重新写规则因环境的不同而有所差异,上诉代码为apache下的实现,nginx下的实现可以找度娘!