PHP路由代码
今天发了些我写的代码,去我空间看就知道了,路由代码需要结合br / div ?php/div div /*配制*//div div $config=array(/div div /* 数据库设置 *//div div DB_TYPE = mysql, // 数据库类型/div div span style=white
今天发了些我写的代码,去我空间看就知道了,路由代码需要结合
<?php
/*配制*/
$config=array(
/* 数据库设置 */
'DB_TYPE' => 'mysql', // 数据库类型
'DB_HOST' => 'localhost', // 服务器地址
'DB_NAME' => 'php', // 数据库名
'DB_USER' => 'root', // 用户名
'DB_PWD' => '123', // 密码
'DB_PREFIX' => 'jiaodu_', // 数据库表前缀
'DB_CHARSET' => 'utf8', // 数据库编码默认采用utf8
/* SESSION设置 */
'SESSION_START'
=> 'user', //session方式,文件方式:file, 数据库设置为user
/* 模板引擎设置 */
'TMPL_ADMIN_PATH'
=>'admin',//后台目录名称
'TMPL_COMPILE_PATH'
=>'/Runtime',//读写目录
'TMPL_PATH'
=>'/template',//模板路径
'TMPL_TEMPLATE_SUFFIX' => 'html', // 默认模板文件后缀
'TMPL_L_DELIM' => '{',
// 模板引擎普通标签开始标记
'TMPL_R_DELIM' => '}',
// 模板引擎普通标签结束标记
'TMPL_STRIP_SPACE' => true, // 是否去除模板文件里面的html空格与换行
'TMPL_CACHE_ON'
=> true, // 是否开启模板编译缓存,设为false则每次都会重新编译,一般用于模板调试
/* URL设置 */
'URL_HTML_SUFFIX' => 'html', // URL伪静态后缀设置
'URL_PATHINFO_MODEL' => 2, // URL模式,1不隐藏、2隐藏入口文件[需要规则支持]
/*其它设置*/
'PASS_ENCRYPT'
=>'www.8now.net',//加密因子
);
<?php
/**
* 路由
* @author 角度 QQ:1286522207
*
*/
class Dispatcher extends Action {
private $url;
private $config;
function __construct(){
$this->routerCheck();
}
/**
* 路由检测
*/
function routerCheck(){
global $config;
$this->config=$config;
$suffix=$this->config['URL_HTML_SUFFIX'];
// 获取当前路由参数对应的变量
$paths = (array_filter(explode('/',trim(preg_replace('/\.'.$suffix.'$/','',$_SERVER['PHP_SELF']),'/'))));
if ($this->config['URL_PATHINFO_MODEL']==1){
define('__URL__','/'.$paths[0].'/');
}elseif ($this->config['URL_PATHINFO_MODEL']==2){
define('__URL__','/');
}
if (empty($paths[1])){
$paths[1]='index';
}
if (empty($paths[2])){
$paths[2]='index';
}
if (empty($paths[3])){
$paths[3]='index';
}
$this->Action($paths);
}
/**
* 实例化操作
*/
function Action($paths){
if ($paths[1]==$this->config['TMPL_ADMIN_PATH']){
include APP_PATH.'/'.$this->config['TMPL_ADMIN_PATH'].'/commonAction.php';
$A=APP_PATH.'/'.$paths[1]."/".$paths[2]."Action.class.php";
$b=$paths[2];
@$c=$paths[3];
}else {
$A=APP_PATH."/Action/".$paths[1]."Action.class.php";
$b=$paths[1];
$c=$paths[2];
}
if (is_file($A)){
include $A;
$action=$b."Action";
@$controller = new $action();
if(method_exists($controller, $c)){
$controller->$c();
}else {
exit('方法不在');
}
}else {
exit('类不在');
}
}
/**
* 解析为$_GET全局变量
*/
function pathinfo(){
$pathinfo=(array_filter(explode('/',$this->url)));
$count=count($pathinfo);
for($foo=1;$foo<$count;$foo+=2){
$_GET[$pathinfo[$foo]]=($foo+2)==$count?array_shift(explode('.',$pathinfo[$foo+1])):$pathinfo[$foo+1];
}
}
/**
* 重排数组
* @param unknown_type $array
*/
function Reorder($array){
$i=0;
foreach ($array as $row){
$a[$i++]=$row;
}
return $a;
}
function __destruct(){
}
}
- 上一篇:超实用的模板引擎
- 下一篇:PHP生成友好URL
精彩图集
精彩文章






