php mvc代码,php mvc框架

$endrun=false;

define("CONTROLLERS_DIR",dirname(__FILE__).'/controllers/');

define("VIEWS_DIR",dirname(__FILE__).'/views/');

define("MODELS_DIR",dirname(__FILE__).'/models/');

define("CONFIG_DIR",dirname(__FILE__).'/config/');

define("CLASSS_DIR",dirname(__FILE__).'/classs/');

define("CONFIG_FILE",'config.php');

if(file_exists(CONFIG_DIR.CONFIG_FILE))

{

require(CONFIG_DIR.CONFIG_FILE);

}

function inti()

{

set_error_handler('error_handler');

register_shutdown_function('shutdown_func');

if(version_compare(PHP_VERSION,'5.4.0','

{

ini_set('magic_quotes_runtime',0);

}

else

{

define('MAGIC_QUOTES_GPC',false);

}

if(get_config('charset')!='error')

{

ini_set('default_charset',get_config('charset'));

}

if(get_config('appname')!='error')

{

header('X-Powered-By:'.get_config('appname'));

}

if(get_config('timezone')!='error')

{

ini_set('date.timezone',get_config('timezone'));

}

if(get_config('debug')!='error')

{

if(get_config('debug')==false)

{

ini_set("display_errors", "off");

}

if(get_config('debug')==true)

{

ini_set("display_errors", "on");

}

}

}

function get_query()

{

$request_url=$_SERVER['REQUEST_URI'];

$a=explode('/',$request_url);

$length=count($a);

if($length<4)

{

return false;

}

else

{

$s=$a[3];

if(strpos($s,'.html')===false)

{

return $s;

}

else

{

return str_replace('.html','',$s);

}

}

}

function get_config($key)

{

if(isset($GLOBALS['config']))

{

if(is_array($GLOBALS['config']))

{

if(array_key_exists($key,$GLOBALS['config']))

{

return $GLOBALS['config'][$key];

}

}

}

return 'error';

}

function is_method($method)

{

$m=$_SERVER['REQUEST_METHOD'];

if(strtoupper($method)==$m)

{

return true;

}

return false;

}

//页面跳转

function redirect($url)

{

header('Location:'.$url);

exit();

}

//载入model

function model($name)

{

$name.='_model';

if(file_exists(MODELS_DIR.$name.'.php'))

{

require_once(MODELS_DIR.$name.'.php');

if(class_exists($name))

{

$c=new $name;

return $c;

}

}

return false;

}

//载入class

function load_class($name)

{

if(file_exists(CLASSS_DIR.$name.'.php'))

{

include(CLASSS_DIR.$name.'.php');

}

}

function error_handler($errno,$errstr,$errfile,$errline)

{

if (!(error_reporting() & $errno))

{

return;

}

echo 'something is wrong!';

}

function shutdown_func()

{

if(!$GLOBALS['endrun'])

{

echo("500 something is wrong!");

}

}

function map()

{

$request_url=$_SERVER['REQUEST_URI'];

if($request_url=='/')

{

instance('index','index');

}

else

{

$a=explode('/',$request_url);

$length=count($a);

$c=$a[1];

if($length>=3)

{

$m=$a[2];

if($m=="")

{

instance($c,$c);

}

else

{

instance($c,$m);

}

}

else

{

instance($c,$c);

}

}

$GLOBALS['endrun']=true;

}

function instance($c,$m)

{

$c.='_controller';

$m.='_action';

if(file_exists(CONTROLLERS_DIR.$c.'.php'))

{

require_once(CONTROLLERS_DIR.$c.'.php');

if(class_exists($c))

{

if(method_exists($c,$m))

{

$r=new $c;

if($c!=$m)

{

$r->$m();

}

}

else

{

echo '404 not found';

}

}

else

{

echo '404 not found';

}

}

else

{

echo '404 not found';

}

}

function json($arr)

{

header('content-type:application/json; charset=utf-8');

if(is_array($arr))

{

echo json_encode($arr);

return;

}

echo json_decode(array('error'=>'parameter error'));

}

function text($str)

{

header('content-type:text/plain; charset=utf-8');

echo $str;

}

function render($view,$data = null)

{

$path=VIEWS_DIR.$view.'.php';

if(file_exists($path))

{

header('content-type:text/html; charset=utf-8');

ob_start();

if(is_array($data))

{

extract($data);

}

require $path;

echo trim(ob_get_clean());

}

else

{

echo 'template not found';

}

}

function run()

{

inti();

map();

}

run();

?>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值