实现一个简单php框架的示例-创新互联-古蔺大橙子建站
RELATEED CONSULTING
相关咨询
选择下列产品马上在线沟通
服务时间:8:30-17:00
你可能遇到了下面的问题
关闭右侧工具栏

新闻中心

这里有您想知道的互联网营销解决方案
实现一个简单php框架的示例-创新互联

小编给大家分享一下实现一个简单php框架的示例,希望大家阅读完这篇文章之后都有所收获,下面让我们一起去探讨吧!

让客户满意是我们工作的目标,不断超越客户的期望值来自于我们对这个行业的热爱。我们立志把好的技术通过有效、简单的方式提供给客户,将通过不懈努力成为客户在信息化领域值得信任、有价值的长期合作伙伴,公司提供的服务项目有:国际域名空间、网站空间、营销软件、网站建设、霞浦网站维护、网站推广。

首先看一下现有的文件目录

实现一个简单php框架的示例

DOCUMENT_ROOR 为 /home/www目录


然后看一下入口文件的内容

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)."
"; call_user_func_array([$currentObj,$action],$params); return; ?>

然后创建一个简单的控制器 user.php,放到applicaiton/controllers/目录下,具体内容如下:

最后测试一个正确的控制器跳转和错误的控制器跳转


首先测试一下正确的流程: 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框架的示例”有了一定的了解,如果想了解更多相关知识,欢迎关注创新互联行业资讯频道,感谢各位的阅读!


文章标题:实现一个简单php框架的示例-创新互联
当前URL:http://scgulin.cn/article/csioos.html