php url 调度
1, 支持非rewrite即: http://localhost/index.php/blog/view/5456-asdf.html 也可以被正确解析。。 ----------------------------------- 2,增加:绝对地址生成 只要 rurl('myFirstRouter', array('id' => '33', 'name' => 'thename'), true); 最后多加一个true,默认为false即相对地址。 生成绝对地址如:网站根目录/fleaphp/test/blog/view/33-thename.html 修改自ZendFramework的Router_Regexp类。 花了点时间整理的,水平有限,希望有高人能完善一下。 定义一个路由跟定义DSN一样的方法:
return array( 'routers' =>array( 'myFirstRouter' => array( 'blog/view(?:/(\d+)-(.+))\.html', array( 'id' => '1', 'controller' => 'default', 'action' => 'index' ), array( 1 => 'id', 2 => 'name' ), 'blog/view/%d-%s.html' ), 'mySecondRouter' => array( 'blog(?:/(\d+)-(.+))\.html', array( 'id' => '1', 'controller' => 'default', 'action' => 'index' ), array( 1 => 'id', 2 => 'name' ), 'blog/%d-%s.html' ), 'myThirdRouter' => array( '([a-z0-9]+)/([a-z0-9]+)', array(), array( 1 => 'controller', 2 => 'action' ), 'blog/%d-%s.html' ) ) ); 复制代码 是一个二维数组,每一个值为一条路由规则。其中第一项是正则表达式,第二项为:参数默认值(这里可以设置controller,action,及其它参数的默认值。) 第三项为:参数的对应关系,与第一项的正则表达里面匹配元素对应。 第四项用于生成链接时候使用的格式。如果没看明白,可以看ZF的Router一节。 先发改的My_Dispatcher_Regexp类的代码:
loadRouters(); if (! is_array($this->_routers)) return false; if (!$this->_pathInfo) $this->getPathInfo(); foreach (array_reverse($this->_routers) as $router) { if (! is_array($router)) continue; if ($router[0] == '' || !is_string($router[0])) continue; $regexp = '#^' . $router[0]. '$#i'; if (! isset($router[1])) $router[1] = array(); if (! isset($router[2])) $router[2] = array(); if ($args = $this->match($regexp, $this->_pathInfo, $router[1], $router[2])) { $this->_curRouter = $router; $data['controller'] = $args['controller']; $data['action'] = $args['action']; $_GET = array_merge($_GET, $args); break; } } $this->_request = $data; } /** * 载入路由数据信息 * */ function loadRouters() { static $routerLoaded; if ($routerLoaded) return; $routerLoaded = false; $routerConfig = FLEA::getAppInf('routerConfig'); FLEA::loadAppInf($routerConfig); $this->_routers = FLEA::getAppInf('routers'); $routerLoaded = true; } /** * 根据服务器环境不同,取得RequestUri信息 * * @return unknown */ function getRequestUri() { if ($this->_requestUri) return $this->_requestUri; if (isset($_SERVER['HTTP_X_REWRITE_URL'])) { // check this first so IIS will catch $requestUri = $_SERVER['HTTP_X_REWRITE_URL']; } elseif (isset($_SERVER['REQUEST_URI'])) { $requestUri = $_SERVER['REQUEST_URI']; } elseif (isset($_SERVER['ORIG_PATH_INFO'])) { // IIS 5.0, PHP as CGI $requestUri = $_SERVER['ORIG_PATH_INFO']; if (!empty($_SERVER['QUERY_STRING'])) { $requestUri .= '?' . $_SERVER['QUERY_STRING']; } } else { $requestUri = null; } $this->_requestUri = $requestUri; return $requestUri; } function getBaseUrl() { if ($this->_baseUrl) return $this->_baseUrl; $filename = basename($_SERVER['SCRIPT_FILENAME']); if (basename($_SERVER['SCRIPT_NAME']) === $filename) { $baseUrl = $_SERVER['SCRIPT_NAME']; } elseif (basename($_SERVER['PHP_SELF']) === $filename) { $baseUrl = $_SERVER['PHP_SELF']; } elseif (isset($_SERVER['ORIG_SCRIPT_NAME']) && basename($_SERVER['ORIG_SCRIPT_NAME']) === $filename) { $baseUrl = $_SERVER['ORIG_SCRIPT_NAME']; // 1and1 shared hosting compatibility } else { // Backtrack up the script_filename to find the portion matching // php_self $path = $_SERVER['PHP_SELF']; $segs = explode('/', trim($_SERVER['SCRIPT_FILENAME'], '/')); $segs = array_reverse($segs); $index = 0; $last = count($segs); $baseUrl = ''; do { $seg = $segs[$index]; $baseUrl = '/' . $seg . $baseUrl; ++$index; } while (($last > $index) && (false !== ($pos = strpos($path, $baseUrl))) && (0 != $pos)); } // Does the baseUrl have anything in common with the request_uri? $requestUri = $this->getRequestUri(); if (0 === strpos($requestUri, $baseUrl)) { // full $baseUrl matches $this->_baseUrl = $baseUrl; return $this->_baseUrl; } if (0 === strpos($requestUri, dirname($baseUrl))) { // directory portion of $baseUrl matches $baseUrl = rtrim(dirname($baseUrl), '/'); $this->_baseUrl = $baseUrl; return $this->_baseUrl; } if (!strpos($requestUri, basename($baseUrl))) { // no match whatsoever; set it blank $this->_baseUrl = ''; return $this->_baseUrl; } // If using mod_rewrite or ISAPI_Rewrite strip the script filename // out of baseUrl. $pos !== 0 makes sure it is not matching a value // from PATH_INFO or QUERY_STRING if ((strlen($requestUri) >= strlen($baseUrl)) && ((false !== ($pos = strpos($requestUri, $baseUrl))) && ($pos !== 0))) { $baseUrl = substr($requestUri, 0, $pos + strlen($baseUrl)); } $baseUrl = rtrim($baseUrl, '/'); $this->_baseUrl = $baseUrl; return $this->_baseUrl; } function getPathInfo () { $baseUrl = $this->getBaseUrl(); if (null === ($requestUri = $this->getRequestUri())) { return null; } // Remove the query string from REQUEST_URI if ($pos = strpos($requestUri, '?')) { $requestUri = substr($requestUri, 0, $pos); } if ((null !== $baseUrl) && (false === ($pathInfo = substr($requestUri, strlen($baseUrl))))) { // If substr() returns false then PATH_INFO is set to an empty string $pathInfo = ''; } elseif (null === $baseUrl) { $pathInfo = $requestUri; } $this->_pathInfo = $pathInfo; return $pathInfo; } /** * Matches a user submitted path with a previously defined route. * Assigns and returns an array of defaults on a successful match. * * @param string Path used to match against this routing map * @return array|false An array of assigned values or a false on a mismatch */ function match($regex, $path, $defaults, $map) { $path = trim(urldecode($path), '/'); $res = preg_match($regex, $path, $values); if ($res === 0) return false; foreach ($values as $i => $value) { if (!is_int($i) || $i === 0) { unset($values[$i]); } } $values = $this->_getMappedValues($map, $values); $defaults = $this->_getMappedValues($map, $defaults, false, true); $return = $values + $defaults; return $return; } /** * Maps numerically indexed array values to it's associative mapped counterpart. * Or vice versa. Uses user provided map array which consists of index => name * parameter mapping. If map is not found, it returns original array. * * Method strips destination type of keys form source array. Ie. if source array is * indexed numerically then every associative key will be stripped. Vice versa if reversed * is set to true. * * @param array Indexed or associative array of values to map * @param boolean False means translation of index to association. True means reverse. * @param boolean Should wrong type of keys be preserved or stripped. * @return array An array of mapped values */ function _getMappedValues($map, $values, $reversed = false, $preserve = false) { if (count($map) == 0) { return $values; } $return = array(); foreach ($values as $key => $value) { if (is_int($key) && !$reversed) { if (array_key_exists($key, $map)) { $index = $map[$key]; } elseif (false === ($index = array_search($key, $map))) { $index = $key; } $return[$index] = $values[$key]; } elseif ($reversed) { $index = (!is_int($key)) ? array_search($key, $map, true) : $key; if (false !== $index) { $return[$index] = $values[$key]; } } elseif ($preserve) { $return[$key] = $value; } } return $return; } /** * Assembles a URL path defined by this route * * @param array An array of name (or index) and value pairs used as parameters * @return string Route path with user submitted parameters */ function assemble($defaults, $map = array(), $reverse, $data = array()) { if ($reverse === null) { return '构建网址失败!路由参数错误!'; } $data = $this->_getMappedValues($map, $data, true, false); $data += $this->_getMappedValues($map, $defaults, true, false); //$data += $this->_values; ksort($data); $return = @vsprintf($reverse, $data); if ($return === false) { return '构建网址失败!'; } return $return; } /** * 使用路由构建网址 */ function url($routerName, $urlOptions, $absolute) { $this->loadRouters(); if (isset($this->_routers[$routerName])) $curRouter = $this->_routers[$routerName]; elseif (isset($this->_curRouter)) $curRouter = $this->_curRouter; if (is_array($curRouter) && count($curRouter) == 4 && is_string($curRouter[3])) { $defaults = $curRouter[1]; $map = $curRouter[2]; $reverse = $curRouter[3]; } else { return '构建网址失败!路由参数错误!'; } if (is_array($map) && is_string($reverse)) if (!$absolute) return $this->assemble($defaults, $map, $reverse, $urlOptions); else { if (!$this->_baseUrl) $this->getBaseUrl(); return $this->_baseUrl . '/' .$this->assemble($defaults, $map, $reverse, $urlOptions); } } } 复制代码 这里要说一个比较好的自定义类的命名规则及文件放置位置。在FLEA下面建一个My的目录里面放自已的类。比如My_Dispatcher_Regexp放在: My/Dispatcher/Regexp.php 同时为方便写一个生成网址助手: My_Helper_Router My/Helper/Router.php 代码如下:
url($routerName, $urlOptions, $absolute); } 复制代码 使用方法:/* 修改默认的Dispatcher为自定义的Dispatcher类*/ FLEA::setAppInf('dispatcher','My_Dispatcher_Regexp'); /* 设置路由配置信息的文件位置*/ FLEA::setAppInf('routerConfig', './APP/config/router.php'); 其它代码跟任何一个普通的例子一样。 controller里面代码如下:
复制代码 我们在view中用下面代码:
'33', 'name' => 'thename')); 复制代码 就可以看到$_GET得到正确的参数,rurl也生成我们期望的网址: blog/view/33-thename.html 绝对网址生成方法如下:
rurl('myFirstRouter', array('id' => '33', 'name' => 'thename'),true); //将生成如下的网址: /other/fleaphp/test/blog/view/33-thename.html 如果没有使用apache的mod_rewrite功能生成的网址如下: /fleaphp/test/index.php/blog/view/33-thename.html |

熱AI工具

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

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

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

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

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

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

Dreamweaver CS6
視覺化網頁開發工具

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

在PHP中,應使用password_hash和password_verify函數實現安全的密碼哈希處理,不應使用MD5或SHA1。1)password_hash生成包含鹽值的哈希,增強安全性。 2)password_verify驗證密碼,通過比較哈希值確保安全。 3)MD5和SHA1易受攻擊且缺乏鹽值,不適合現代密碼安全。

PHP和Python各有優勢,選擇依據項目需求。 1.PHP適合web開發,尤其快速開發和維護網站。 2.Python適用於數據科學、機器學習和人工智能,語法簡潔,適合初學者。

PHP在電子商務、內容管理系統和API開發中廣泛應用。 1)電子商務:用於購物車功能和支付處理。 2)內容管理系統:用於動態內容生成和用戶管理。 3)API開發:用於RESTfulAPI開發和API安全性。通過性能優化和最佳實踐,PHP應用的效率和可維護性得以提升。

PHP是一種廣泛應用於服務器端的腳本語言,特別適合web開發。 1.PHP可以嵌入HTML,處理HTTP請求和響應,支持多種數據庫。 2.PHP用於生成動態網頁內容,處理表單數據,訪問數據庫等,具有強大的社區支持和開源資源。 3.PHP是解釋型語言,執行過程包括詞法分析、語法分析、編譯和執行。 4.PHP可以與MySQL結合用於用戶註冊系統等高級應用。 5.調試PHP時,可使用error_reporting()和var_dump()等函數。 6.優化PHP代碼可通過緩存機制、優化數據庫查詢和使用內置函數。 7

PHP類型提示提升代碼質量和可讀性。 1)標量類型提示:自PHP7.0起,允許在函數參數中指定基本數據類型,如int、float等。 2)返回類型提示:確保函數返回值類型的一致性。 3)聯合類型提示:自PHP8.0起,允許在函數參數或返回值中指定多個類型。 4)可空類型提示:允許包含null值,處理可能返回空值的函數。

PHP仍然具有活力,其在現代編程領域中依然佔據重要地位。 1)PHP的簡單易學和強大社區支持使其在Web開發中廣泛應用;2)其靈活性和穩定性使其在處理Web表單、數據庫操作和文件處理等方面表現出色;3)PHP不斷進化和優化,適用於初學者和經驗豐富的開發者。

PHP和Python各有優劣,選擇取決於項目需求和個人偏好。 1.PHP適合快速開發和維護大型Web應用。 2.Python在數據科學和機器學習領域佔據主導地位。

PHP適合web開發,特別是在快速開發和處理動態內容方面表現出色,但不擅長數據科學和企業級應用。與Python相比,PHP在web開發中更具優勢,但在數據科學領域不如Python;與Java相比,PHP在企業級應用中表現較差,但在web開發中更靈活;與JavaScript相比,PHP在後端開發中更簡潔,但在前端開發中不如JavaScript。
