define ('ROOT_DIR', realpath(dirname(dirname(dirname(__FILE__)))));
require_once LIB_DIR.DS.'configs'.DS.'global.config.php';
require_once LIB_DIR.DS.'configs'.DS.'function.config.php';
require_once APP_DIR.DS.'router.php';
require_once APP_DIR . DS .'autoload.php';
$_RequestUri = $_SERVER['REQUEST_URI'];
//print_r($_SERVER);
$_DocumentPath = $_SERVER['DOCUMENT_ROOT'];
$_FilePath = __FILE__;
$_AppPath = str_replace($_DocumentPath,'', $_FilePath);
$urlPath = $_RequestUri;
// 兼容 ? 號方式的參數
$urlPathArr = explode('?', $urlPath);
$urlPath = $urlPathArr[0];
// $wParams=array();
// if(isset($urlPathArr['1']))
// {
// $wParams=$urlPathArr['1'];
// }
$wParamsArr = array_merge($_GET, $_POST);
$appPathArr = explode(DS, $_AppPath);
for($i =0; $i < count($appPathArr); $i++){
$p = $appPathArr[$i];
if($p){
$urlPath = preg_replace('/^\/'. $p .'\//','/', $urlPath,1);
}
}
$urlPath = preg_replace('/^\//','', $urlPath,1);
$appPathArr = explode("/", $urlPath);
$appPathArrCount = count($appPathArr);
$arr_url = array(
'modules'=>'index',
'controller'=>'index',
'method'=>'index',
'parms'=> array(),
);
if(!empty($appPathArr[0])){
$arr_url['modules']= $appPathArr[0];
}
if(!empty($appPathArr[1])){
$arr_url['controller']= $appPathArr[1];
}
if(!empty($appPathArr[2])){
$arr_url['method']= $appPathArr[2];
}
$arr_url['parms']= $wParamsArr;
if($appPathArrCount >3){
// 若是大於三 則說明後面是參數
for($i =3; $i < $appPathArrCount; $i +=2){
$arr_temp_hash = array(strtolower($appPathArr[$i])=> $appPathArr[$i +1]);
$arr_url['parms']= array_merge($arr_url['parms'], $arr_temp_hash);
}
}
//print_r($arr_url);
// 轉入 controller
$module_name = $arr_url['modules'];
//$class_name = NAME_DIR.DIRECTORY_SEPARATOR.'Controllers'.DIRECTORY_SEPARATOR.ucfirst($module_name).DIRECTORY_SEPARATOR.ucfirst($arr_url['controller']).'Controller';
$class_name = NAME_DIR .'\Controllers\\'. ucfirst($module_name).'\\'. ucfirst($arr_url['controller']).'Controller';
$controller_name = $arr_url['controller'];
$controller_file = CONTROLLERS_DIR . DS . ucfirst($module_name). DS . ucfirst($controller_name).'Controller.php';
//print_r($class_name);
$method_name = $arr_url['method'];
if(file_exists($controller_file)){
//print_r($controller_file);
include $controller_file;
//spl_autoload_register( array($class_name,$method_name) );
//echo 32;die;
$requestArr['path']= $arr_url['modules']. DS . $arr_url['controller']. DS . $arr_url['method'];
$requestArr['server_name']= $_SERVER['SERVER_NAME'];
$requestArr['method']= $_SERVER['REQUEST_METHOD'];
$obj_module =new $class_name($arr_url['parms'], $requestArr);
if(!method_exists($obj_module, $method_name)){
die("要調用的方法不存在");
}else{
//print_r($arr_url['parms']);
$obj_module->$method_name();
}
}else{
die("定義的類不存在");
}
$this->params= $this->_addslashes($paramsArr);
$this->autoDeCode();
$this->request = $requestArr;
// //判斷是否登陸
$path ="/".strtolower($this->getPath());
$this->mtoken=$this->session('mtoken');
if(empty($this->mtoken)&& substr($path,0,11)!='/user/login'){
$url ='/user/login';
echo '<script type="text/javascript">top.window.location.href="'.$url.'";</script>';
}
namespaceApplication;
/**
* 自動加載類
* @author kinmos
*/
classAutoloader
{
// 應用的初始化目錄,做爲加載類文件的參考目錄
protectedstatic $_appInitPath ='';
/**
* 設置應用初始化目錄
* @param string $root_path
* @return void
*/
publicstaticfunction setRootPath($root_path)
{
self::$_appInitPath = $root_path;
}
/**
* 根據命名空間加載文件
* @param string $name
* @return boolean
*/
publicstaticfunction loadByNamespace($name)
{
// 相對路徑
$class_path = str_replace('\\', DIRECTORY_SEPARATOR ,$name);
// 先嚐試在應用目錄尋找文件
if(self::$_appInitPath)
{
$class_file =self::$_appInitPath . DIRECTORY_SEPARATOR . $class_path.'.php';
}
//print_r($class_file);
// 文件不存在,則在上一層目錄尋找
if(empty($class_file)||!is_file($class_file))
{
$class_file = APP_DIR.DIRECTORY_SEPARATOR ."$class_path.php";
}
// 找到文件
if(is_file($class_file))
{
// 加載
require_once($class_file);
//print_r($class_file);
if(class_exists($name,false))
{
//echo $name;
returntrue;
}
}
returnfalse;
}
}
// 設置類自動加載回調函數
spl_autoload_register('\Application\Autoloader::loadByNamespace');
<?php namespace Config; /** * mysql配置 * @author */ class Db { // public static $default = array( 'host' => '192.168.0.88', 'port' => 3306, 'user' => 'root', 'password' => '123456', 'dbname' => 'kinwork', 'charset' => 'utf8', ); }
配置着本地數據庫的路徑,另外 這個文件是能夠擴展的。便於後面的分庫以及分佈式的部署javascript
另外在Lib目錄中添加了三個數據庫操做文件php
Db 文件,DbTable DbConnection java
Db 是操做數據庫的表層文件,DbTable 文件是實際進行數據庫操做的類 DbConnection 是進行數據庫連接的類 進行數據庫鏈接池的 管理mysql
而後在咱們的Models 文件夾中添加了 Db文件夾 對應每一個數據庫表的一個文件 sql
<?php namespace Http\Models\Db; use Lib\DbTable; /** * */ class User extends DbTable { protected $_name = 'user'; protected $_primary = 'id'; protected $_db = 'default'; /* * 構造函數 */ public function __construct() { } }
其中 _name 是表名 _db 是對應的config的數據庫配置 兼容數據庫的分庫應用shell
在實際使用的時候,數據庫
$DbTable_User = Db::DbTable('User'); // 查詢全部數據 $list=$DbTable_User->getList('1=1'); print_r($list); // 查詢 分頁數據 $pagelist=$DbTable_User->pageList('1=1','*'); print_r($pagelist); // 查詢單條數據 $row=$DbTable_User->getRow(array('id'=>1)); print_r($row); // 添加數據 $id=$DbTable_User->insert(array('name'=>'kinmos')); print_r($id); // 修改數據 $status=$DbTable_User->update(array('name'=>'kinmos'),array('id'=>1)); print_r($status); // 刪除數據 $status=$DbTable_User->delete(array('id'=>15)); print_r($status);
至此,數據層完成。數組
publicfunction getViewUrl(){
$this->urlPath = $this->getPath();
$Path_arr = explode("/",$this->urlPath);
$Module = isset($Path_arr[0])&&$Path_arr[0]!=''?$Path_arr[0]:"Index";
$Controller = isset($Path_arr[1])&&$Path_arr[1]!=''?$Path_arr[1]:"Index";
$Method = isset($Path_arr[2])&&$Path_arr[2]!=''?$Path_arr[2]:"index";
return ucfirst($Module).'/'.ucfirst($Controller).'/'.$Method;
}
/*
* 跳到視圖頁面
* $data = array() 數組格式 帶到視圖頁面的參數
*/
publicfunction view($data = array()){
$view = VIEWS_DIR . DS.$this->getViewUrl().'.php';
extract($data, EXTR_OVERWRITE);
ob_start();
file_exists($view)?require $view :exit($view .' 不存在');
$content = ob_get_contents();
return $content;
}
<?php include_once(VIEWS_DIR .DS."Pub".DS."header.php");?>
publicfunction _addslashes($param_array){
if(is_array($param_array)){
foreach($param_array as $key=>$value){
if(is_string($value))
$param_array[$key]= addslashes(trim($value));
}
}else{
$param_array = addslashes($param_array);
}
return $param_array;
}
functionEncode($str)
{
if($str ==''){
return'';
}
$aes =newAes();
return urlencode($aes->encrypt($str));
}
functionDecode($str)
{
if($str ==''){
return'';
}
$aes =newAes();
if(strpos($str,'%'))
return $aes->decrypt(urldecode($str));
else
return $aes->decrypt($str);
}
classAes
{
private $_privateKey ="kinmoshelloword&*#";
private $_byt = array(0x19,0x34,0x56,0x78,0x90,0xAB,0xCD,0xEF,0x12,0x34,0x56,0x78,0x90,0xAB,0xCD,0xEF);
publicfunction toStr($bytes)
{
$str ='';
foreach($bytes as $ch){
$str .= chr($ch);
}
return $str;
}
publicfunction encrypt($data)
{
$vi = $this->toStr($this->_byt);
$encrypted = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $this->_privateKey, $data, MCRYPT_MODE_CBC, $vi);
return base64_encode($encrypted);
}
publicfunction decrypt($data)
{
$vi = $this->toStr($this->_byt);
$encryptedData = base64_decode($data);
$decrypted = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $this->_privateKey, $encryptedData, MCRYPT_MODE_CBC, $vi);
//print_r($decrypted);
return rtrim($decrypted,"\0");
}
}
protectedfunction autoDeCode(){
if(isset($this->params["id"]))
{
$id=$this->params["id"];
//print_r($id);die;
//print_r($_GET);
if(!empty($id)&&!preg_match("/^[0-9]+$/",$id)){
$this->params["id"]=Decode($id);
}
}
//自動解密ids
if(isset($this->params["ids"]))
{
$ids=$this->params["ids"];
if(!empty($ids)){
$newids="";
//print_r($idsarr);
if(is_array($ids))
foreach($ids as $key => $value){
if(!preg_match("/^[0-9]+$/",$value)){
if($newids!="")
$newids.=','.Decode($value);
else
$newids=Decode($value);
//$this->params["ids"]=
}
}
else
{
if(!empty($ids)&&!preg_match("/^[0-9]+$/",$ids)){
$newids=Decode($ids);
}
}
$this->params["ids"]=$newids;
}
}
}