[php]-MVC
[php]-MVC
- MVC介紹
- MVC結構
- url訪問控制器
- 訪問視圖層
- 頁面跳轉
- 函數助手
MVC介紹
MVC(Model View Controller)是軟件工程中的一種軟件架構模式,它把軟件系統分為模型、視圖和控制器三個基本部分。用一種業務邏輯、數據、界面顯示分離的方法組織代碼,將業務邏輯聚集到一個部件里面,在改進和個性化定制界面及用戶交互的同時,不需要重新編寫業務邏輯。
M:Model,模型完成具體的業務操作,如:查詢數據庫,封裝對象V:view視圖。JSP、HTML等來進行數據展示C:Controller控制器。 Servlet獲取View的請求調用模型將數據交給視圖進行展示MVC結構
目錄
|——app: 應用目錄(多模塊)|——單模塊: 即一個主界面,其中包含Model、View、Controller|——多模塊: 即一個主界面,其中包含Model、View、Controller |——config: 配置文件目錄 |——pro: 核心目錄文件 |——public: WEB目錄文件url訪問控制器
先在public下創建index.php
然后訪問
http://127.0.0.1:3678/public/index.php/可以訪問得到
如果訪問 app/index.php 可以直接修改url地址 , 所以我們切換目錄文件
在網站根目錄下添加 public 目錄
訪問 /app/ 就會報錯了
在 app 下寫個控制器
然后訪問控制器里面的方法
http://127.0.0.1:3678/index.php/index/home/index?id=123&titile=456我們可以找到這些東西
[SCRIPT_NAME] => /index.php [REQUEST_URI] => /index.php/index/home/index?id=123&titile=456 [QUERY_STRING] => id=123&titile=456 // /public/index.php $script_name=$_SERVER['SCRIPT_NAME']; // /public/index.php/index/home/index?id=123&titile=456 $request_url=$_SERVER['REQUEST_URI']; // id=123&titile=456 $query_string=$_SERVER['QUERY_STRING'];然后提取我們想要的部分 /index/home/index?
... $url = str_replace($script_name, '' ,$request_url); $url = str_replace($query_string,'',$url); echo $url;然后去除前面的 / 和后面的 ?
... $url = str_replace($script_name, '' ,$request_url); $url = str_replace($query_string,'',$url); $url = ltrim($url,'/'); $url = rtrim($url,'?'); echo $url;然后變成數組的形式
$url = explode('/',$url); print_r($url);接著各區所需
$model = $url[0]; $controller = $url[1]; $action = $url[2];先定義一個路徑
define("APP_PATH",dirname(__DIR__)); ... $path = APP_PATH.'/app/'.$model.'/controller/'.$controller.'.php'; echo $path; ...實例化控制器 訪問方法
... define("APP_PATH",dirname(__DIR__)); $path = APP_PATH.'/app/'.$model.'/controller/'.$controller.'.php'; require_once $path; $new = new $controller; $new ->$action(); ... <?php define("APP_PATH",dirname(__DIR__));// /public/index.php $script_name=$_SERVER['SCRIPT_NAME']; // /public/index.php/index/home/index?id=123&titile=456 $request_url=$_SERVER['REQUEST_URI']; // id=123&titile=456 $query_string=$_SERVER['QUERY_STRING']; $url = str_replace($script_name, '' ,$request_url); $url = str_replace($query_string,'',$url); $url = ltrim($url,'/'); $url = rtrim($url,'?'); $url = explode('/',$url);$model = $url[0]; $controller = $url[1]; $action = $url[2];$path = APP_PATH.'/app/'.$model.'/controller/'.$controller.'.php'; require_once $path; $new = new $controller; $new ->$action();然后優化一下目錄訪問路徑
/public/index.php
... if(count($url) >= 3){$model = $url[0];$controller = $url[1];$action = $url[2]; } else {$model = 'index';$controller = 'Home';$action = 'index'; } .../config/app.php
<?php return['default_module' => 'index','default_controller' => 'Home','defalut_action' => 'index', ];然后替換代碼
/public/index.php
... else{$config = require_once APP_PATH . '/config/app.php';$model = $config['default_model'];$controller = $config['default_controller'];$action = $config['default_action']; }然后訪問http://127.0.0.1:3678/index.php/
但其實不需要很多入口文件,所以這里就把原本的index.php入口文件構建到核心文件Pro中,除此之外如果count >=3時,若訪問不存在的控制器例如/index/index/index,他還是會報錯,所以通過下邊的三個if語句,再次進行優化
/Pro/Pro.php
<?php class Pro {public static function init(){$script_name = $_SERVER['SCRIPT_NAME']; // /public/index.php$request_url = $_SERVER['REQUEST_URI']; // /public/index.php/index/home/index?id=123&titile=456$query_string = $_SERVER['QUERY_STRING']; // id=123&titile=456// 需要 index/home/index$url = str_replace($script_name,'',$request_url); // /index/home/index?id=123&titile=456$url = str_replace($query_string,'',$url); // /index/home/index?$url = ltrim($url,'/'); // index/home/index?$url = rtrim($url,'?'); // index/home/index$url = explode('/',$url);if(count($url) >= 3){$model = $url[0];$controller = $url[1];$action = $url[2];} else {$config=require_once APP_PATH.'/config/app.php';$model = $config['default_model'];$controller = $config['default_controller'];$action = $config['default_action'];}//判斷應用模塊if(!is_dir(APP_PATH.'/app/'.$model)){exit("應用模塊<".$model.">不存在!");}$path = APP_PATH.'/app/'.$model.'/controller/'.$controller.'.php';//判斷控制器if(!file_exists($path)){exit("控制器<".$controller.">不存在!");}require_once $path;$new = new $controller;//判斷方法if(!method_exists($new,$action)){exit("方法< ".$action ." >不存在");}return ['new'=> $new,'action'=>$action];} }/Pro/App.php
<?php define('APP_PATH',dirname(__DIR__)); require_once APP_PATH.'/Pro/Pro.php';$init = Pro::init(); $new = $init['new']; $action = $init['action']; $new->$action();/public/index.php
<?php define('APP_PATH',dirname(__DIR__)); require_once APP_PATH.'/Pro/App.php'; ?>此時就可以通過 index.php,訪問到核心文件的入口了,訪問不存在的應用模塊、控制器、方法時會返回提示信息,如下圖訪問方法 abc 時,則會輸出提示信息
http://127.0.0.1:3678/index.php/index/home/abc訪問視圖層
/app/index/view/home/index.php
<?php echo "This is view";此時只需要修改Home.php的index方法,便可直接訪問到視圖層
<?phpclass Home{public function index(){require_once APP_PATH .'/app/index/view/home/index.php';}然后把核心的代碼寫入到核心文件里
/Pro/Pro.php
<?php class Pro {public static function init(){$script_name = $_SERVER['SCRIPT_NAME']; // /public/index.php$request_url = $_SERVER['REQUEST_URI']; // /public/index.php/index/home/index?id=123&titile=456$query_string = $_SERVER['QUERY_STRING']; // id=123&titile=456// 需要 index/home/index$url = str_replace($script_name,'',$request_url); // /index/home/index?id=123&titile=456$url = str_replace($query_string,'',$url); // /index/home/index?$url = ltrim($url,'/'); // index/home/index?$url = rtrim($url,'?'); // index/home/index$url = explode('/',$url);if(count($url) >= 3){$model = $url[0];$controller = $url[1];$action = $url[2];} else {$config=require_once APP_PATH.'/config/app.php';$model = $config['default_model'];$controller = $config['default_controller'];$action = $config['default_action'];}define('MODEL',$model);//判斷應用模塊if(!is_dir(APP_PATH.'/app/'.$model)){exit("應用模塊<".$model.">不存在!");}$path = APP_PATH.'/app/'.$model.'/controller/'.$controller.'.php';//判斷控制器if(!file_exists($path)){exit("控制器<".$controller.">不存在!");}require_once $path;$new = new $controller;//判斷方法if(!method_exists($new,$action)){exit("方法< ".$action ." >不存在");}return ['new'=> $new,'action'=>$action];}public static function view($path,$data){$path = APP_PATH .'/app/'.MODEL.'/view/'.$path.'.php';require_once $path;} }/app/index/controller/Home.php
<?php class Home{// require_once APP_PATH .'/app/index/view/home/index.php';public function index(){Pro::view('home/index','test_test');} }/app/index/view/home/index.php
<?= $data ?>此時在訪問就是我們傳入的 test_test 值
頁面跳轉
先創建個跳轉文件 /Pro/Load.php
<!DOCTYPE html> <html lang="en"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width,initial-scale=1.0"><title>Document</title> </head> <body><h1><?=$msg?></h1><h3>頁面自動跳轉,等待 <b id="msg">3</b>秒,<a href="javascript:li()">跳轉</a></h3><script>var msg = document.getElementById("msg");var i = 3;setInterval(function(){i--;msg.innerHTML = i;if(i<=0){window.location.href= "../<?= $path?>";}},1000);function li(){window.location.href = "../<?= $path ?>";}</script> </body></html>Pro/Pro.php ,創建load方法
public static function load($path,$msg){$url=APP_PATH.'/Sentiment/Load.php';require_once $url; }/app/index/controller/Home.php 通過abc()方法跳轉到index方法調用view
<?php class Home{public function index(){Sentiment::view('home/index','test_test');}public function abc(){Sentiment::load('home/index',"Muz1");} }訪問 /index.php/index/home/abc
函數助手
Pro/Common.php,將之前寫的view、load函數寫到其中
<?php function dump($data){echo "<pre>";print_r($data);echo "</pre>"; }function view($path,$data){Pro::view($path,$data); }function load($path,$msg){Pro::load($path,$msg); }在/Pro/App.php,進行文件包含
require_once APP_PATH.'/Pro/Common.php';這時/app/index/controller/Home.php,就不需要通過 Pro::view() 這種方式調用了,而是直接 view(); 即可
除此外dump方法便于輸出數組,當index方法中,是數組時調用dump
<?phpclass Home {public function index(){$data = ['Muz1' => 1,'test' => 2];dump($data);}public function abc(){Pro::load('home/index','Muz1');} }結果
運行流程是
/public/index.php -->require_once APP_PATH.'/Pro/App.php'; /Pro/App.php -->require_once APP_PATH.'/Pro/Pro.php';在Pro.php獲取控制器和方法值 /app/index/controller/Home.php(由于App.php包含了Common.php,所以就不用通過::調用方法)總結
- 上一篇: 单页面网站如何高效做SEO优化?
- 下一篇: python爬虫的线程、进程、异步的基础