如何在百度提交网站,百度指数如何分析,毕业设计模板网站,永久免费crm管理系统最近想学习php的mvc结构#xff0c;在网上找了一些资料#xff0c;可是大部分都是相同的#xff0c;或者写的不是很满意。接着看了一个cakephp的框架#xff0c;感觉太多的函数了#xff0c;让人觉得就是一个记函数的过程#xff0c;所以觉得不是很好。 我对mvc的理解是在网上找了一些资料可是大部分都是相同的或者写的不是很满意。接着看了一个cakephp的框架感觉太多的函数了让人觉得就是一个记函数的过程所以觉得不是很好。 我对mvc的理解是c负责分配任务协调m和v。用户发出请求时c把请求分配给模型m模型从数据库处理查询或者其他的一些数据操作然后把数据返回给cc再把数据传递给相应的v再在前台显示。这有个比喻较好当我们去餐馆吃饭时招待×××就相当于c她把客户的请求传达给厨师m厨师把做好的菜再给××××××把菜再端给顾客。。。。。。。 现在我把自己的做的一个mvc介绍下 我的访问网址是这种类型http://www.example.com/index.php?controller/action/id:2/page:3 其中controller是控制器的名字action是控制器下的动作名称而action以后的参数如id:2,page:3等是额外的参数我们称之为params。 好了接下来就是怎么解析这个URL把它分化成controlleractionparams。 首先说一下目录结构 index.php 这是入口文件用户要访问的每一个页面都要经过它 .htaccess 这是.htaccess文件 app app目录 app/controllers 控制器目录其中文件名都是nameController的形式类名和文件名相同类中的动作名为nameAction的 形式 app/views 视图的目录这里我借用smarty模板 app/views/cache app/views/templates 模板文件 app/views/confgs app/views/templates_c 以上三个目录是与smarty一致的具体可以参考smarty手册 app/models 模型存放目录 core 核心文件目录 core/ini.php 初始化处理 core/config.php 一些配置文件 core/dbconfig.php 数据库的配置文件 core/dispatcher.php 分发路由 core/router.php URL处理文件 core/loader.php core/initializer.php 包含一些文件 core/lib core/lib/appController.php 父控制器 core/lib/appView.php 视图类主要继承smarty文件 core/lib/appModel.php 父模型类连接数据库 core/lib/dataAccess.php 数据库的一些基本操作 core/lib/smarty smary的核心文件完全照搬smarty的libs文件但将其中的Smarty.class.php重命名为Smarty.php主要是为了自动化类。 以上就是要用到的目录文件 下面看index.php //index,php ?php include(core/ini.php); initializer::initialize(); $router loader::load(router); dispatcher::dispatch($router); ? 先慢慢解释。。。。。。。。。。 看ini.php //ini.php ?php header(Content-type:text/html;charsetutf-8); set_include_path(get_include_path() . PATH_SEPARATOR . core); //将要用到的文件包含进来 function __autoload($object){ require_once({$object}.php); } ? 其中__autoload函数很重要当我们要初期化类时如果当前文件没有它就会自动去包含文件中寻找该类。在这里我把类名和文件名设为相同这比较方便比如initializer::initialize();此时就会去包含文件中查找initializer.php文件然后初始化类initializer再调用它的静态方法initialize(); 接着看看initializer.php //initializer.php ?php class initializer { public static function initialize() { set_include_path(get_include_path().PATH_SEPARATOR . core/lib); set_include_path(get_include_path().PATH_SEPARATOR . app/controllers); set_include_path(get_include_path().PATH_SEPARATOR.app/models); set_include_path(get_include_path().PATH_SEPARATOR.app/views); set_include_path(get_include_path().PATH_SEPARATOR.core/lib/smarty); } } ? 这是一个把我们将要用到的文件包含进来目的是方便我们初始化类方便。 //loader.php ?php class loader { private static $loaded array(); public static function load($object){ $valid array( library, view, model, helper, router, config, hook, cache, db); if (!in_array($object,$valid)){ throw new Exception(Not a valid object {$object} to load); } if (empty(self::$loaded[$object])){ self::$loaded[$object] new $object(); } return self::$loaded[$object]; } } ? //router.php ?php class router { private $route; private $controller; private $action; private $params; public function __construct() { $path array_keys($_GET);//取得URL以后的字符串controller/action/id:2/page:3 if (!isset($path[0])){ $path[0] index;//如果没有控制器名称。则调用index控制器 } $route $path[0]; $this-route $route; $routeParts preg_split(,$route);//用“/划分进数组 $this-controller$routeParts[0];//控制器名称 $this-actionisset($routeParts[1])? $routeParts[1]:index;//动作名称默认是index array_shift($routeParts); array_shift($routeParts); $a$routeParts; $barray(); foreach($a as $aa) {$cexplode(:,$aa); $b[$c[0]]isset($c[1])?$c[1]:$c[0]; } $this-params$b;//此时为数组的形式Array ( [id] 2 [page] 3 ) } public function getAction() { if (empty($this-action)) $this-actionindex; return $this-action; } public function getController() { return $this-controller; } public function getParams() { return $this-params; } } ? 再看看dispatcher.php //dispatcher.php ?php class dispatcher { public static function dispatch($router) { ob_start();//打开缓冲区 $controller ($router-getController()).Controller;//控制器类或者控制器的文件名 $action ($router-getAction()).Action;//控制器的动作函数名 $params $router-getParams(); //print_r($params); $controllerfile app/controllers/{$controller}.php; if (file_exists($controllerfile)){ require_once($controllerfile); $app new $controller();//实例化该控制器 $app-params$params; $app-$action();//调用该动作函数 $output ob_get_clean(); echo $output;//输出到浏览器 }else{ throw new Exception(Controller not found); } } } ? 下面看看index控制器怎么样 //indexController.php ?php class indexController extends appController { public function indexAction() { $this-view-assign(test,hello,world!); $this-view-display(index.tpl); } } ? 这里有一个appController父类我们看看它怎么样 //appController.php ?php class appController { var $model; var $view; var $params; function __construct() { $this-modelnew appModel(); $this-viewnew appView(); } } ? appController主要初始化父类模型和视图所以接着看看appModel类和appView类是个什么样 //appView.php ?php class appView extends Smarty { //var $smarty; function __construct() { $this-smarty; $this-template_dir app/views/templates/; $this-compile_dir app/views/templates_c/; $this-config_dir app/views/configs/; $this-cache_dir app/views/cache/; } } ? appView主要就是重新包装一下smarty模板 //appModel.php ?php require_once(/dbconfig.php); class appModel extends dataAccess { function __construct() {parent::__construct(DB_HOST,DB_USER,DB_PASSWORD,DB_DATABASE); } } ? 这里负责一些数据库的链接操作其中dbconfig.php为 //dbconfig.php ?php define(DB_HOST,127.0.0.1); define(DB_USER,root); define(DB_PASSWORD,min); define(DB_DATABASE,sdw); define(CHAR_SET,utf8); ? appModel又继承了类文件dataAccess.php这是数据库处理的一些文件包括数据库链接查询等等。。。。。这里就不列出了。 所以现在来看indexController中的动作indexAction中的 $this-view-assign(test,hello,world); 和$this-view-display(index.tpl);他们分别是调用父类appController的$this-view类而$this-view类就是appView类即smarty类所以smarty类的assign和display方法应该很熟悉的。 在app/views/templates下建立文件index.tcp //index.tcp {$test} 转载于:https://blog.51cto.com/sexy1ei/1043692