一个php框架的简略实现,仅实现简略路由层
发布时间:01/01 来源:未知 浏览:
关键词:
引荐:《PHP视频教程》
第一看一下现有的文件名目
DOCUMENT_ROOR 为 /home/www名目
然后看一下入口文件的内容
<?php $controll_action = $_GET['_ca_']; $params = explode('/',$controll_action); $params_count = count($params); $otherParams = $params; if($params_count>1) { $controller = $params[0]; $action = $params[1]; unset($params[0]); unset($params[1]); }else if($params_count==1) { $controller = $params[0]; $action = 'index'; unset($params[0]); } $filename = strtolower($controller).'.php'; $controller_path = $_SERVER['DOCUMENT_ROOT'].'/application/controllers/'; if(!file_exists($controller_path.$filename)) { throw new Exception('controller '.$filename.' is not exists!'); return; } include($controller_path.$filename); $classname = ucfirst($controller); if(!class_exists($classname)) { throw new Excpetion('class '.$classname.' is not exists!'); } $reflecObj = new ReflectionClass($classname); if(!$reflecObj->hasMethod($action)){ throw new Exception('method '.$action.' is not exists!'); } $currentObj = new $classname(); echo "classname=$classname,action=$action,params=".json_encode($params)."<br/>"; call_user_func_array([$currentObj,$action],$params); return; ?>
然后创立一个简便的操纵器 user.php,放到applicaiton/controllers/名目下,详细内容如下:
<?php class User { function __construct(){ } public function index($name='') { echo 'hello,'.$name.',lucky,you are arrive here!'; } }
最后测试一个准确的操纵器跳转和错误的操纵器跳转
第一测试一下准确的流程: http://192.168.1.99/user/index/xiaoming
输出内容:
classname=User,action=index,params={"2":"xiaoming"} hello,xiaoming,lucky,you are arrive here!
再测试一下不存在的操纵器,http://192.168.1.99/account/index/xiaoming
Fatal error: Uncaught exception 'Exception' with message 'controller acount.php is not exists!' in /home/www/webroot/index.php:25Stack trace:#0 {main} thrown in /home/www/webroot/index.php on line 25
以上就是一个php框架的简便实现,仅实现简便路由层的具体内容,更多请关注百分百源码网其它相关文章!