私は自分の軽量MVCフレームワークをPHPのために働いています。私はある時点でオープンソースライセンスの下でリリースするかもしれません。ここで私のPHP MVCフレームワークでのルーティング
は私がルートを処理するために使用されているものです:
function routes($routes, $uriPath) {
// Loop through every route and compare it with the URI
foreach ($routes as $route => $actualPage) {
// Create a route with all identifiers replaced with ([^/]+) regex syntax
// E.g. $route_regex = shop-please/([^/]+)/moo (originally shop-please/:some_identifier/moo)
$route_regex = preg_replace('@:[^/][email protected]', '([^/]+)', $route);
// Check if URI path matches regex pattern, if so create an array of values from the URI
if(!preg_match('@' . $route_regex . '@', $uriPath, $matches)) continue;
// Create an array of identifiers from the route
preg_match('@' . $route_regex . '@', $route, $identifiers);
// Combine the identifiers with the values
$this->request->__get = array_combine($identifiers, $matches);
array_shift($this->request->__get);
return $actualPage;
}
// We didn't find a route match
return false;
}
$ルートは、このような形式に渡された配列である:
$routes = array(
// route => actual page
'page/:action/:id' => 'actualPage',
'page/:action' => 'actualPage',
)
$ uriPathは大手将来をせずにURIパスですスラッシュ私のページコントローラでページ/更新/ 102
私はそうのようなルート情報にアクセスすることができます
echo $this->request->__get['action'];
// update
echo $this->request->__get['id'];
// 102
私の質問は基本的に「これを簡略化または最適化することができますか?」されています。正規表現とpreg_replaceとpreg_match呼び出しの数を単純化することに特に重点を置いています。
あなたはパフォーマンスの問題を持っていますか?ここを見てくださいhttp://kohanaframework.org/3.2/guide/api/Route –