PHP從零開始打造自己的MVC框架之路由類實現(xiàn)方法分析
本文實例講述了PHP從零開始打造自己的MVC框架之路由類實現(xiàn)方法。分享給大家供大家參考,具體如下:
在core目錄下,新建一個名為lib的子目錄,然后把我們前面寫個route.php這個文件移動到這個目錄下。
因為route類文件路徑修改,所以在實例化的時候:
new \core\lib\route();
然后我們來完善route.php:
<?php namespace core\lib; class Route { public $controller; // 控制器 public $action; // 方法(動作) public function __construct() { // xxx.com/index.php/index/index // xxx.com/index.php/index /* * 1.隱藏index.php * 2.獲取URL 參數(shù)部分 * 3.返回對應(yīng)控制器和方法 * */ if(isset($_SERVER['REQUEST_URI']) && $_SERVER['REQUEST_URI'] != '/'){ // 處理成這種格式:index/index $path = $_SERVER['REQUEST_URI']; $pathArr = explode('/',trim($path,'/')); if(isset($pathArr[0])){ $this->controller = $pathArr[0]; } unset($pathArr[0]); if(isset($pathArr[1])){ $this->action = $pathArr[1]; unset($pathArr[1]); }else{ $this->action = 'index'; } // url多余部分(參數(shù)部分)轉(zhuǎn)換成 GET // id/1/str/2 $count = count($pathArr) + 2; $i = 2; while($i < $count){ if(isset($pathArr[$i + 1])){ $_GET[$pathArr[$i]] == $pathArr[$i + 1]; } $i = $i + 2; } p($_GET); // 打印GET }else{ $this->controller = 'index'; // 默認控制器 $this->action = 'index'; // 默認方法 } } }
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《php面向?qū)ο蟪绦蛟O(shè)計入門教程》、《PHP數(shù)組(Array)操作技巧大全》、《PHP基本語法入門教程》、《PHP運算與運算符用法總結(jié)》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對大家PHP程序設(shè)計有所幫助。
相關(guān)文章
PHP封裝的數(shù)據(jù)庫保存session功能類
這篇文章主要介紹了PHP封裝的數(shù)據(jù)庫保存session功能類,涉及php基于SessionHandlerInterface接口實現(xiàn)的讀取、寫入、保存、銷毀等常用操作方法,需要的朋友可以參考下2016-07-07ThinkPHP中調(diào)用PHPExcel的實現(xiàn)代碼
本文介紹ThinkPHP中處理導(dǎo)出成Excel文件的一個PHP庫,PHPExcel。它可以很容易的生成出一個完整的、復(fù)雜的Excel文件,需要的朋友可以參考下2017-04-04