推荐:《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
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。