一步步编写PHP的Framework(十二)
我上次在讲redirect和forward的时候我就说过,这两个函数要正常使用还需要修改一下Route这个类,至少要将比如域名,控制器名,Action名等存储起来,后面调用redirect,forward的时候可以使用。
现在我们就转到Route.php,原来这个类的代码很简单:
01 |
02 | class Route extends Base { |
03 | public static function run() { |
04 | $controller= empty($_GET['c']) ? C('defaultController') : trim($_GET['c']); //设置了默认的控制器 |
05 | $action = empty($_GET['a']) ? C('defaultAction') : trim($_GET['a']); //设置了默认的Action |
06 | $controllerBasePath = APP_PATH . '/UserApps/Modules/Controllers/'; |
07 | $controllerFilePath = $controllerBasePath . $controller . 'Controller.php'; |
08 | if(is_file($controllerFilePath)) { |
09 | include $controllerFilePath; |
10 | $controllerName = $controller . 'Controller'; |
11 | if(class_exists($controllerName)) { |
12 | $controllerHandler = new $controllerName(); |
13 | if(method_exists($controllerHandler,$action)) { |
14 | $controllerHandler->$action(); |
15 | } else { |
16 | echo 'the method does not exists'; |
17 | } |
18 | } else { |
19 | echo 'the class does not exists'; |
20 | } |
21 | } else { |
22 | echo 'controller not exists'; |
23 | } |
24 | } |
25 | } |
现在我们需要将域名取出来,那怎么弄呢?
实际上PHP有一个强大的超全局变量$_SERVER,很多信息都存储在这里面,我们可以查看一下:
1 |
2 | var_dump($_SERVER); |
我们注意到这里面有一个 HTTP_HOST属性,查看PHP手册,这么写的:
Contents of the Host: header from the current request, if there is one.
假设现在有一个URL:http://localhost/test/test.php,那$_SERVER['HTTP_HOST']的值为什么呢,实际上为localhost。一般来说,我们想取到的是localhost/test,那么怎么获取后面的/test呢?
我们继续搜索一下:
发现REQUEST_URI,SCRIPT_FILENAME,SCRIPT_NAME,PHP_SELF的值都为/test/test.php,查询PHP手册解释分别为:
1. The URI which was given in order to access this page; for instance, '/index.html'
2. The absolute pathname of the currently executing script.
3.Contains the current script's path. This is useful for pages which need to point to themselves. The __FILE__ constant contains the full path and filename of the current (i.e. included) file.
4. The filename of the currently executing script, relative to the document root. For instance, $_SERVER['PHP_SELF'] in a script at the address http://example.com/test.php/foo.bar would be /test.php/foo.bar. The __FILE__ constant contains the full path and filename of the current (i.e. included) file. If PHP is running as a command-line processor this variable contains the script name since PHP 4.3.0. Previously it was not available.
我们发现REQUEST_URI比较靠谱,当然,我这个地方测试的是apache的情况,nginx,iis等还有在.htaccess文件设置了rewrite规则后又不一样,如果真要写一个好的Route,考虑的东西会非常多的,针对于URL的普通模式,PATHINFO模式,REWRITE模式,兼容模式,我们使用最普通的方式。
首先我们定义一个存储路径的类,Path.php:
01 |
02 | class Path extends Base { |
03 | private static $_base = ''; |
04 | private static $_controller = ''; |
05 | private static $_action = ''; |
06 | public static function setBasePath($base) { |
07 | self::$_base = $base; |
08 | } |
09 | public static function setController($controller) { |
10 | self::$_controller = $controller; |
11 | } |
12 | public static function setAction($action) { |
13 | self::$_action = $action; |
14 | } |
15 | public static function getBasePath() { |
16 | return self::$_base; |
17 | } |
18 | public static function getController() { |
19 | return self::$_controller; |
20 | } |
21 | public static function getAction() { |
22 | return self::$_action; |
23 | } |
24 | } |
就像Java中pojo,这个类只有setter和getter,我就不多讲了。
然后再看看Route.php,首先还是获取URL,怎么获取呢?
1 | $_SERVER['HTTP_HOST'] . substr($_SERVER['REQUEST_URI'],0,strrpos($_SERVER['REQUEST_URI'],'/')) |
由于之前已经讲了HTTP_HOST和REQUEST_URI的作用了,这段代码主要就说一下后面的substr和strrpos,substr就是截断字符串,strrpos是获取某一个子字符串在父字符串中最后一次出现的位置。
PS:我这样写得还是有问题的,但是为了简便,不弄复杂了。
然后就是将这些值存储到Path中,
1 | Path::setBasePath($_SERVER['HTTP_HOST'] . substr($_SERVER['REQUEST_URI'],0,strrpos($_SERVER['REQUEST_URI'],'/'))); |
2 | Path::setController($controller); |
3 | Path::setAction($action); |
设置了这些参数之后,在Controller.php中的redirect和forward的代码也要稍做修改:
01 |
02 | class Controller extends Base { |
03 | protected function _redirect(Array $arr) { |
04 | array_key_exists('controller',$arr) $arr['controller'] = Path::getContrller(); |
05 | array_key_exists('action',$arr) $arr['action'] = Path::getAction();; |
06 | $str = 'http://' . Path::getBasePath() . '/index.php?'; |
07 | foreach($arr as $key => $val) { |
08 | if(!is_int($key)) { |
09 | $str .= ($key . '=' . $val . '&'); |
10 | } |
11 | } |
12 | $str = substr($str,0,strlen($str) - 1); |
13 | Response::redirect($str); |
14 | } |
15 | protected function _forward(Array $arr) { |
16 | $controller = Path::getController(); |
17 | $action = Path::getAction(); |
18 | if(array_key_exists('controller',$arr)) { |
19 | $controller = $arr['controller']; |
20 | } |
21 | if(array_key_exists('action',$arr)) { |
22 | $action = $arr['action']; |
23 | } |
24 | $controller .= 'Controller'; |
25 | if($controller === get_class()) { |
26 | if(method_exists($this,$action)) { |
27 | $this->$action(); |
28 | } else { |
29 | //时间有限,不写逻辑了 |
30 | } |
31 | } else { |
32 | if(class_exists($controller)) { |
33 | $class = new $controller(); |
34 | if(method_exists($class,$action)) { |
35 | $class->$action(); |
36 | } else { |
37 | //时间有限,不写了 |
38 | } |
39 | } else { |
40 | //时间有限,不写了 |
41 | } |
42 | } |
43 | } |
44 | protected function _assign(Array $arr) { |
45 | View::assign($arr); |
46 | } |
47 | protected function _display($str) { |
48 | if(is_string($str)) { |
49 | $str = str_replace(array( |
50 | '.','#' |
51 | ),array( |
52 | '/','.' |
53 | ),$str); |
54 | View::display(MODULES_PATH . View::VIEW_BASE_PATH . $str . '.php'); |
55 | } |
56 | } |
57 | } |
这个里面主要的改动就是控制器和Action的获取变成了调用Path类的方法,还有_redirect中,$str = 'http://' . Path::getBasePath() . '/index.php?',这里我假设使用的时http协议,并且不存在rewrite,服务器采用的是apache。
搞定之后再使用_redirect和_forward,发现是不是没有问题了?

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

熱門話題

「你的組織要求你更改PIN訊息」將顯示在登入畫面上。當在使用基於組織的帳戶設定的電腦上達到PIN過期限制時,就會發生這種情況,在該電腦上,他們可以控制個人設備。但是,如果您使用個人帳戶設定了Windows,則理想情況下不應顯示錯誤訊息。雖然情況並非總是如此。大多數遇到錯誤的使用者使用個人帳戶報告。為什麼我的組織要求我在Windows11上更改我的PIN?可能是您的帳戶與組織相關聯,您的主要方法應該是驗證這一點。聯絡網域管理員會有所幫助!此外,配置錯誤的本機原則設定或不正確的登錄項目也可能導致錯誤。即

Windows11將清新優雅的設計帶到了最前沿;現代介面可讓您個性化和更改最精細的細節,例如視窗邊框。在本指南中,我們將討論逐步說明,以協助您在Windows作業系統中建立反映您的風格的環境。如何更改視窗邊框設定?按+開啟“設定”應用程式。 WindowsI前往個人化,然後按一下顏色設定。顏色變更視窗邊框設定視窗11「寬度=」643「高度=」500「>找到在標題列和視窗邊框上顯示強調色選項,然後切換它旁邊的開關。若要在「開始」功能表和工作列上顯示主題色,請開啟「在開始」功能表和工作列上顯示主題

預設情況下,Windows11上的標題列顏色取決於您選擇的深色/淺色主題。但是,您可以將其變更為所需的任何顏色。在本指南中,我們將討論三種方法的逐步說明,以更改它並個性化您的桌面體驗,使其具有視覺吸引力。是否可以更改活動和非活動視窗的標題列顏色?是的,您可以使用「設定」套用變更活動視窗的標題列顏色,也可以使用登錄編輯程式變更非活動視窗的標題列顏色。若要了解這些步驟,請前往下一部分。如何在Windows11中變更標題列的顏色? 1.使用「設定」應用程式按+開啟設定視窗。 WindowsI前往“個人化”,然

您是否在Windows安裝程式頁面上看到「出現問題」以及「OOBELANGUAGE」語句? Windows的安裝有時會因此類錯誤而停止。 OOBE表示開箱即用的體驗。正如錯誤提示所表示的那樣,這是與OOBE語言選擇相關的問題。沒有什麼好擔心的,你可以透過OOBE螢幕本身的漂亮註冊表編輯來解決這個問題。快速修復–1.點選OOBE應用底部的「重試」按鈕。這將繼續進行該過程,而不會再打嗝。 2.使用電源按鈕強制關閉系統。系統重新啟動後,OOBE應繼續。 3.斷開系統與網際網路的連接。在脫機模式下完成OOBE的所

工作列縮圖可能很有趣,但它們也可能分散注意力或煩人。考慮到您將滑鼠懸停在該區域的頻率,您可能無意中關閉了重要視窗幾次。另一個缺點是它使用更多的系統資源,因此,如果您一直在尋找一種提高資源效率的方法,我們將向您展示如何停用它。不過,如果您的硬體規格可以處理它並且您喜歡預覽版,則可以啟用它。如何在Windows11中啟用工作列縮圖預覽? 1.使用「設定」應用程式點擊鍵並點選設定。 Windows按一下系統,然後選擇關於。點選高級系統設定。導航至“進階”選項卡,然後選擇“效能”下的“設定”。在「視覺效果」選

在Windows11上的顯示縮放方面,我們都有不同的偏好。有些人喜歡大圖標,有些人喜歡小圖標。但是,我們都同意擁有正確的縮放比例很重要。字體縮放不良或圖像過度縮放可能是工作時真正的生產力殺手,因此您需要知道如何自訂以充分利用系統功能。自訂縮放的優點:對於難以閱讀螢幕上的文字的人來說,這是一個有用的功能。它可以幫助您一次在螢幕上查看更多內容。您可以建立僅適用於某些監視器和應用程式的自訂擴充功能設定檔。可以幫助提高低階硬體的效能。它使您可以更好地控制螢幕上的內容。如何在Windows11

螢幕亮度是使用現代計算設備不可或缺的一部分,尤其是當您長時間注視螢幕時。它可以幫助您減輕眼睛疲勞,提高易讀性,並輕鬆有效地查看內容。但是,根據您的設置,有時很難管理亮度,尤其是在具有新UI更改的Windows11上。如果您在調整亮度時遇到問題,以下是在Windows11上管理亮度的所有方法。如何在Windows11上變更亮度[10種方式解釋]單一顯示器使用者可以使用下列方法在Windows11上調整亮度。這包括使用單一顯示器的桌上型電腦系統以及筆記型電腦。讓我們開始吧。方法1:使用操作中心操作中心是訪問

Windows上的啟動過程有時會突然轉向顯示包含此錯誤代碼0xc004f069的錯誤訊息。雖然啟動程序已經聯機,但一些運行WindowsServer的舊系統可能會遇到此問題。透過這些初步檢查,如果這些檢查不能幫助您啟動系統,請跳到主要解決方案以解決問題。解決方法–關閉錯誤訊息和啟動視窗。然後,重新啟動電腦。再次從頭開始重試Windows啟動程序。修復1–從終端啟動從cmd終端啟動WindowsServerEdition系統。階段–1檢查Windows伺服器版本您必須檢查您使用的是哪種類型的W
