深さに任意のフレームワークを実装するものです。ここで私は何をすべきかです:
使用MOD_REWRITE私はparams
を呼び出す$_GET
変数のパラメータ(/some/path/{info}
など)を保存するために:
RewriteRule ^(.+)(\?.+)?$ index.php?params=$1 [L,QSA]
パラメータを解析し、世界的にこの機能を使用してそれらを保存します:
public static function parseAndGetParams() {
// get the original query string
$params = !empty($_GET['params']) ? $_GET['params'] : false;
// if there are no params, set to false and return
if(empty($params)) {
return false;
}
// append '/' if none found
if(strrpos($params, '/') === false) $params .= '/';
$params = explode('/', $params);
// take out the empty element at the end
if(empty($params[count($params) - 1])) array_pop($params);
return $params;
}
動的に適切なページへのルート:
// get the base page string, must be done after params are parsed
public static function getCurPage() {
global $params;
// default is home
if(empty($params))
return self::PAGE_HOME;
// see if it is an ajax request
else if($params[0] == self::PAGE_AJAX)
return self::PAGE_AJAX;
// see if it is a multi param page, and if not, return error
else {
// store this, as we are going to use it in the loop condition
$numParams = count($params);
// initialize to full params array
$testParams = $params;
// $i = number of params to include in the current page name being checked, {1, .., n}
for($i = $numParams; $i > 0; $i--) {
// get test page name
$page = strtolower(implode('/', $testParams));
// if the page exists, return it
if(self::pageExists($page))
return $page;
// pop the last param off
array_pop($testParams);
}
// page DNE go to error page
return self::PAGE_ERROR;
}
}
ここ値は、それが少なくとも特定のページへ最も具体的ページを探すということです。また、フレームワークの外側のワークアウトは、あなたを与える完全に制御ので、バグがどこかにありますならば、あなたはそれを修正することができます知っている - あなたはあなたの枠組みの中でいくつかの奇妙な回避策をルックアップする必要はありません。
これで$params
がグローバルになったので、パラメータを使用するページでは単に$params[X]
が呼び出されて動作します。フレンドリーなURLはフレームワークなし。
私はその後、私はpageExists($page)
呼び出しで見ている配列に入れているページを追加方法。AJAX呼び出しのために
場合、私は特別なに入れる:
// if ajax, include it and finish
if($page == PageHelper::PAGE_AJAX) {
PageHelper::includeAjaxPage();
$db->disconnect();
exit;
}
出来上がり - あなた自身のマイクロルーティングフレームワーク。
[URLの書き換え](http://en.wikipedia.org/wiki/URL_rewriting)についてお聞きしますか? –
はい、私はそれがその名前であることを知らなかった –
私は元の言葉に戻りました。あなたのような概念に精通していないユーザーを考え、彼と同じ言葉を検索します。現在の文言を保持し、URLの書き換えが何であるかを説明してください。 –