php框架的制作原理
index.php 主入口文件 run();?>---------------------------------------------------------------------------------------------
init.php 文件----------------------------------------------------------------------------------------------
config.php 文件1,//url模式,1为普通模式,2为path_info模式? 'default'=>'welcome',//默认的控制器? 'default_action'=>'index'//默认的方法? );?>-----------------------------------------------------------------------------------------------
controller.class.php 文件?analysis();? //开始解析url获得请求的控制器和方法? $control = $_get['con'];? $action = $_get['act'];? $action = ucfirst($action);? //这里构造出控制器文件的路径? $controlfile = root_path . '/controllers/' . $control . '.class.php';? if(!file_exists($controlfile)) //如果文件不存在提示错误, 否则引入? {?? exit({$control}.class.php控制器不存在
. 请检查: .$controlfile.是否存在
);? }? include($controlfile);? $class = ucfirst($control); //将控制器名称中的每个单词首字母大写,来当作控制器的类名? if(!class_exists($class)) //判断类是否存在, 如果不存在提示错误?{?exit({$control}.class.php中未定义的控制器类 . $class);?}?$instance = new $class(); //否则创建实例?if(!method_exists($instance, $action)) //判断实例$instance中是否存在$action方法, 不存在则提示错误?{?exit($class类中不存在方法:. $action);?}?$instance->$action();?}????protected function analysis()?{?//$globals['c']['url_mode'];?global $c; //包含全局配置数组, 这个数组是在config.ph文件中定义的,global声明$c是调用外部的?if($c['url_mode'] == 1)?//如果url模式为1 那么就在get中获取控制器, 也就是说url地址是这种的 [url=http://localhost/index.php?c]http://localhost /index.php?c[/url]=控制器&a=方法?{?$control = !empty($_get['con']) ? trim($_get['con']) : '';?$action = !empty($_get['act']) ? trim($_get['act']) : '';?}?else if($c['url_mode'] == 2) //如果为2 那么就是使用path_info模式, 也就是url地址是这样的 ? ?[url=http://localhost/index.php/]http://localhost/index.php/[/url]控制器/方法 /其他参数?{?if(isset($_server['path_info']))?{?//$_server['path_info']url地址中文件名后的路径信息, 不好理解, 来看看例子?//比如你现在的url是 [url=http://www.php100.com/index.php]http://www.php100.com/index.php[/url] 那么你的$_server['path_info']就是空的?//但是如果url是 [url=http://www.php100.com/index.php/abc/123]http://www.php100.com/index.php/abc/123[/url]?//现在的$_server['path_info']的值将会是 index.php文件名称后的内容 /abc/123/?$path = trim($_server['path_info'], '/');?$paths = explode('/', $path);?$control = array_shift($paths);?$action = array_shift($paths);?}?}?//这里判断控制器的值是否为空, 如果是空的使用默认的?$_get['con'] = !empty($control) ? $control : $c['default'];?//和上面一样?$_get['act'] = !empty($action) ? $action : $c['default_action'];?}}?>--------------------------------------------------------------------------------------------------
welcome.class.php 文件?
?
转自:http://blog.sina.com.cn/s/blog_6ec912d80101916t.html