我需要從路由文件中檢查有效路由,我希望爲動態url部分放置通配符(或佔位符)。 路由器閱讀該JSON格式的所有路由:替換通配符進行網址比較
{"action" : "[email protected]", "method" : "GET", "url" : "showPost/id/{}"}
時發生之比較與當前值來改變架{}任何,也許允許把{}任何標籤內正則表達式,我需要。
這樣的網址: showPost/id/211必須與showPost/id/{}進行比較,並且應該返回true。如果可能的話,我想允許{'[0-9] \'}作爲可選參數,以確保真實值匹配正則表達式。
這樣做的最佳解決方案是什麼?
的comparsison方法是這樣的:
public static function findAction($query) {
foreach (Router::getInstance()->routes as $route) {
if ($route->url == $query) {
return $route;
}
}
}
的$查詢包含/ showPost/ID/221和路由器::的getInstance() - > routes->爲route-> URL包含showPost/ID/{ }
該帖子與此自動解決的問題有關: how to make nice rewrited urls from a router 我不重新發布路由器代碼以避免重複。
在此先感謝