初めて、PHP+Smartyの組み合わせでシステムを作ったさい、
「index.phpにすべてのパラメーターをGETで渡すのは、美しくない」
と思い、URIの一部を.htaccessに任せる事にしました。
.htaccess
RewriteEngine on RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^([0-9A-Za-z]+)/([0-9A-Za-z)]+)/([0-9A-Za-z)]+)/?$ index.php?action=$1&controller=$2&id=$3&%{QUERY_STRING} [L] RewriteRule ^([0-9A-Za-z]+)/([0-9A-Za-z)]+)/?$ index.php?action=$1&controller=$2&%{QUERY_STRING} [L] RewriteRule ^([0-9A-Za-z]+)/?$ index.php?action=$1&controller=index&%{QUERY_STRING} [L]
最後の3行が、URIを「action」「controller」「id」に変換するルールです。
例えば
- 「http://hoge.jp/main」→「http://hoge.jp/index.php?action=main&controller=index」
- 「http://hoge.jp/main/login」→「http://hoge.jp/index.php?action=main&controller=login」
- 「http://hoge.jp/main/view/1」→「http://hoge.jp/index.php?action=main&controller=view&id=1」
このようになります。
後は、index.phpの処理で「switch~case」で分岐させれば簡易静的URLの完成です。
思想的には、MVCフレームワークの劣化版といった感じでしょうか。