b2core怎么通过get传值中文乱码

[代码全屏查看]-b2Core : 300 行的php MVC 架构
[1].[代码] b2core.php
* B2Core 是由 Brant ()发起的基于PHP的MVC架构
* 核心思想是在采用MVC框架的基础上最大限度的保留php的灵活性
* Vison 2.0
define('VERSION','2.0');
// 载入配置文件:数据库、url路由等等
require(APP.'config.php');
// 如果配置了数据库则载入
if(isset($db_config)) $db = new db($db_config);
// 获取请求的地址兼容 SAE
$uri = '';
if(isset($_SERVER['PATH_INFO'])) $uri = $_SERVER['PATH_INFO'];
if(isset($_SERVER['ORIG_PATH_INFO'])) $uri = $_SERVER['ORIG_PATH_INFO'];
if(isset($_SERVER['SCRIPT_URL'])) $uri = $_SERVER['SCRIPT_URL'];
// 去除Magic_Quotes
if(get_magic_quotes_gpc()) // Maybe would be removed in php6
function stripslashes_deep($value)
$value = is_array($value) ? array_map('stripslashes_deep', $value) : (isset($value) ? stripslashes($value) : null);
$_POST = stripslashes_deep($_POST);
$_GET = stripslashes_deep($_GET);
$_COOKIE = stripslashes_deep($_COOKIE);
// 执行 config.php 中配置的url路由
foreach ($route_config as $key =& $val)
$key = str_replace(':any', '([^\/.]+)', str_replace(':num', '([0-9]+)', $key));
if (preg_match('#^'.$key.'#', $uri))$uri = preg_replace('#^'.$key.'#', $val, $uri);
// 获取URL中每一段的参数
$uri = rtrim($uri,'/');
$seg = explode('/',$uri);
$des_dir = $dir = '';
/* 依次载入控制器上级所有目录的架构文件 __construct.php
* 架构文件可以包含当前目录下的所有控制器的父类,和需要调用的函数
foreach($seg as $cur_dir)
$des_dir.=$cur_dir."/";
if(is_file(APP.'c'.$des_dir.'__construct.php')) {
require(APP.'c'.$des_dir.'__construct.php');
$dir .=array_shift($seg).'/';
/* 根据 url 调用控制器中的方法,如果不存在返回 404 错误
* 默认请求 class home-&index()
$dir = $dir ? $dir:'/';
array_unshift($seg,NULL);
= isset($seg[1])?$seg[1]:'home';
$method = isset($seg[2])?$seg[2]:'index';
if(!is_file(APP.'c'.$dir.$class.'.php'))show_404();
require(APP.'c'.$dir.$class.'.php');
if(!class_exists($class))show_404();
if(!method_exists($class,$method))show_404();
$B2 = new $class();
call_user_func_array(array(&$B2, $method), array_slice($seg, 3));
/* B2 系统函数
* load($path,$instantiate) 可以动态载入对象,如:控制器、Model、库类等
* $path 是类文件相对 app 的地址
* $instantiate 为 False 时,仅引用文件,不实例化对象
* $instantiate 为数组时,数组内容会作为参数传递给对象
function &load($path, $instantiate = TRUE )
$param = FALSE;
if(is_array($instantiate)) {
$param = $
$instantiate = TRUE;
$file = explode('/',$path);
$class_name = array_pop($file);
$object_name = md5($path);
static $objects = array();
if (isset($objects[$object_name])) return $objects[$object_name];
require(APP.$path.'.php');
if ($instantiate == FALSE) $objects[$object_name] = TRUE;
if($param)$objects[$object_name] = new $class_name($param);
$objects[$object_name] = new $class_name();
return $objects[$object_name];
/* 调用 view 文件
* function view($view,$param = array(),$cache = FALSE)
* $view 是模板文件相对 app/v/ 目录的地址,地址应去除 .php 文件后缀
* $param 数组中的变量会传递给模板文件
* $cache = TRUE 时,不像浏览器输出结果,而是以 string 的形式 return
function view($view,$param = array(),$cache = FALSE)
if(!empty($param))extract($param);
ob_start();
if(is_file(APP.$view.'.php')) {
require APP.$view.'.php';
echo 'view '.$view.' desn\'t exsit';
// Return the file data if requested
if ($cache === TRUE)
$buffer = ob_get_contents();
@ob_end_clean();
// 取得 url 的片段,如 url 是 /abc/def/g/
seg(1) = abc
function seg($i)
return isset($seg[$i])?$seg[$i]:
// 写入日志
function write_log($level = 0 ,$content = 'none')
file_put_contents(APP.'log/'.$level.'-'.date('Y-m-d').'.log', $content , FILE_APPEND );
// 显示404错误
function show_404() //显示 404 错误
header("HTTP/1.1 404 Not Found");
// 调用 模板 v/404.php
view('v/404');
B2Core 系统类 */
// 抽象的控制器类,建议所有的控制器均基层此类或者此类的子类
function index()
echo "基于 B2 v".VERSION." 创建";
// 数据库操作类
class db {
var $last_
function __construct($conf)
$this-&link = mysql_connect($conf['host'],$conf['user'], $conf['password']);
if (!$this-&link) {
die('无法连接: ' . mysql_error());
return FALSE;
$db_selected = mysql_select_db($conf['default_db']);
if (!$db_selected) {
die('无法使用 : ' . mysql_error());
mysql_query('set names utf8',$this-&link);
//执行 query 查询,如果结果为数组,则返回数组数据
function query($query)
$ret = array();
$this-&last_query = $
$result = mysql_query($query,$this-&link);
if (!$result) {
echo "DB Error, could not query the database\n";
echo 'MySQL Error: ' . mysql_error();
echo 'Error Query: ' . $
if($result == 1 )return TRUE;
while($record = mysql_fetch_assoc($result))
$ret[] = $
function insert_id() {return mysql_insert_id();}
// 执行多条 SQL 语句
function muti_query($query)
$sq = explode(";\n",$query);
foreach($sq
if(trim($s)!= '')$this-&query($s);
// 模块类,封装了通用CURD模块操作,建议所有模块都继承此类。
function __construct()
$this-&db = $
$this-&key = 'id';
public function __call($name, $arg) {
return call_user_func_array(array($this, $name), $arg);
// 向数据库插入数组格式数据
function add($elem = FALSE)
$query_list = array();
if(!$elem)$elem = $_POST;
foreach($this-&fields as $f) {
if(isset($elem[$f])){
$elem[$f] = addslashes($elem[$f]);
$query_list[] = "`$f` = '$elem[$f]'";
$this-&db-&query("insert into `$this-&table` set ".implode(',',$query_list));
return $this-&db-&insert_id();
// 删除某一条数据
function del($id)
$this-&db-&query("delete from `$this-&table` where ".$this-&key."='$id'");
// 更新数据
function update($id , $elem = FALSE)
$query_list = array();
if(!$elem)$elem = $_POST;
foreach($this-&fields as $f) {
if(isset($elem[$f])){
$elem[$f] = addslashes($elem[$f]);
$query_list[] = "`$f` = '$elem[$f]'";
$this-&db-&query("update `$this-&table` set ".implode(',',$query_list)." where ".$this-&key." ='$id'" );
// 统计数量
function count($where='')
$this-&db-&query("select count(*) as a from `$this-&table` where 1 $where");
return $res[0]['a'];
/* get($id) 取得一条数据 或
get($postquery = '',$cur = 1,$psize = 30) 取得多条数据
function get()
$args = func_get_args();
if(is_numeric($args[0])) return $this-&__call('get_one', $args);
return $this-&__call('get_many', $args);
function get_one($id)
$id = is_numeric($id)?$id:0;
$this-&db-&query("select * from `$this-&table` where ".$this-&key."='$id'");
if(isset($res[0]))return $res[0];
function get_many($postquery = '',$cur = 1,$psize = 30)
$cur = $cur & 0 ?$cur:1;
$start = ($cur - 1) * $
$query = "select * from `$this-&table` where 1 $postquery limit $start , $psize";
return $this-&db-&query($query);
[2].[代码] config.php
// 所有配置内容都可以在这个文件维护
error_reporting(E_ERROR);
if(file_exists(APP.'config_user.php')) require(APP.'config_user.php');
// 配置url路由
$route_config = array(
'/reg/'=&'/user/reg/',
'/logout/'=&'/user/logout/',
define('BASE','/');
/* 数据库默认按照 SAE 进行配置 */
$db_config = array(
'host'=&SAE_MYSQL_HOST_M.':'.SAE_MYSQL_PORT,
'user'=&SAE_MYSQL_USER,
'password'=&SAE_MYSQL_PASS,
'default_db'=&SAE_MYSQL_DBb2Core 目录结构
├── app/ 应用目录
│&& ├── b2core.php b2core 核心文件
│&& ├── config.php b2core 配置文件
│&& ├── c/ 控制器目录
│&& ├── m/ 模块类目录
│&& ├── v/ 模板文件目录
│&& └── lib/ 其他类库目录
├── index.php 入口文件
└── .htaccess apache配置文件
核心类和函数
类: c 控制器
所有的web 请求最终都将解析为对控制器类中方法的请求。
其他控制器可以直接继承 c ,或者完全独立也可。
未制定控制器时,默认调用 home 类。
未指定方法时,默认调用 index 方法。
如访问 /, 将调用 app/c/home.php 文件 class home 中的& function index()
支持多级目录。
浏览器请求 /good/yes/ 将会调用
class good 中的 yes 方法。
如果存在 app/c/good/ 目录,且存在 app/c/good/__construct.php 文件, 将会调用
app/c/good/yes.php 文件中的 class yes& 中的function index() 方法。
类:& db 数据库操作
封装了基本的数据库操作。分别如下:
db::query($sql)
执行一句SQL 语句,如果有数据返回,则将数据转化为数组并返回。
db::muti_query($sql)
执行多句SQL语句。语句之间需要用';' 分隔。
db::insert_id()
返回最近一个插入操作生成的 id
类:m 模块
所有模块需继承 class m 类。具体方法有:
m::add( array $arr ) 将 $arr 插入到数据表中
m::update( int $id, array $arr ) 更新 id 为 $id 的数据 为 $arr
m::del(int $id) 删除 $id 数据
m::get() 方法,get 方法有以下两种使用方法:
- m::get(int $id) , 返回 $id 数据
- m::get(str $postquery , int $cur = 1,int $psize = 30) 返回符合 $postquery 查询条件,以 $pagesize 条记录为1页的,第 $cur 页的数据
函数: load()
load 函数可以加载 模块类、控制器类、以及类库。 如:
$school = load('m/school');
将加载 m/school.php 文件中的 school 模块类,并赋予给 $school 变量。
$school = load('c/school');
将加载 c/school.php 文件中的 school 控制器类,并赋予给 $school 变量。
$school = load('lib/school');
将加载 lib/school.php 文件中的 school 类库,并赋予给 $school 变量。
以上方法在加载类的同时实例化类。 也可以仅仅通过 load 函数引用类而不实例化。 方法如下
load('lib/school', false);
将仅仅引用 lib/school 文件;
你可以可以传递变量到需要调用的类。如:
$param = array(1,'asdf'); $school = load('lib/school',$param);
lib/school.php 中的
school 类的初始化方法, construct(1,'asdf');
函数: view( 模板名,变量数组 , 是否暂不输出)
调用 app/v/ 目录下的模板, 如:
view('app/school/list',$param);
将调用 app/v/school/list.php简单、快速、灵活的 php mvc 框架!
这是一个超轻量级的架构。它的核心代码不到300行。包括 3个核心类,6个核心函数。 它可以被用做学习 PHP MVC 架构的快速入门。
少写点 (Write Less)
一个核心文件
无冗余代码
保留php的灵活性
封装了CRUD基础操作,简单配置即可使用
实时加载lib类
可以根据需要扩展核心类
包含常用的分页、验证等代码,可以直接调用
又来一个MVC架构?
原因是这样的,我以前一直使用CodeIgniter (CI) 的构架,觉得很不错。但是也发现了一些问题,在解决的过程中,形成了目前的构架。这是一个超轻量级的架构。它的核心代码不到200行。包括 3个核心类,2个核心函数。 它可以被用做学习 PHP MVC 架构的快速入门。如果你用过 CI 或者类似的构架看过代码后你可以马上上手。
B2比CI的优点:
更小更快,核心代码300行,都在一个文件里面。
没有CI的诸多限制(如:GET),更加灵活
有一些针对SEO的小细节
可以调用 Codeigniter 的 Lib 包
为什么要开源?
使用方显价值
希望能够共同完善代码
保持它的超轻量级
如果你能让他在实现目前功能的前提下变的更小
严谨的修正一些潜在缺陷
新版本更新如下:
1. 修复部分 bug
2. 简化 load 函数使用, controler , model, lib 采用相同方式调用
$file = load('path/file'); 实例化 app/path/file.php 中的类。
3. 修改view 函数, view 函数调用文件不再默认 app/v 目录。
项目地址:b2Core 3.0
相关最新源码
b2Core是一个简单、快速、灵活的,超轻量级的phpMVC架构。它的核心代码不到300行。包括 3个核心类,6个核心函数。 它可以被用做学习 PHP MVC 架构的快速入门。
b2Core比CI的优点:
更小更快,核心代码300行,都在一个文件里面。
没有CI的诸多限制(如:GET),更加灵活
有一些针对SEO的小细节
可以调用Codeigniter的Lib包
&&&&&&&&&&&&&&&&&&&&&&&&&
源码下载地址

我要回帖

更多关于 get 传参 的文章

 

随机推荐