據我所知,有 '路由' 的兩種不同的方式,並使用 '友好的URL'REQUEST_URI VS .htaccess的
1:獨資使用的.htaccess:
RewriteRule ^foobar/([^/]+)/([^/]+)$ "index.php?foo=$1&bar=$2" [NC]
或 2:使用htaccess的與一個的index.php '路由' 系統結合:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase/
# if file not exists
RewriteCond %{REQUEST_FILENAME} !-f
# if dir not exists
RewriteCond %{REQUEST_FILENAME} !-d
# avoid 404s of missing assets in our script
RewriteCond %{REQUEST_URI} !^.*\.(jpe?g|png|gif|css|js)$ [NC]
RewriteRule .* index.php [QSA,L]
</IfModule>
然後內部的index.php:
$uri = explode("/",substr($_SERVER['REQUEST_URI'],1));
if((isset($uri[0])) && ($uri[0]!="")) {
$page = $uri[0];
if(is_file(ROOT."/subs/docs/$page/config.php")) {
include(ROOT."/subs/docs/$page/config.php");
}
} else {
$page="home";
}
然後在$行下面包含$頁面。
我的問題是,哪種方式更好,還是有其他方法我不知道?更好的說,我的意思是在效率,速度和邏輯方面。
這對你更有效嗎? –
另一種將兩者混合的方法是前端控制器,其中所有%{REQUEST_URI}都被路由到單個文件,然後將$ _SERVER ['REQUEST_URI']分成幾部分以找出哪些PHP類將處理與URL。無論效率與否,我都不知道。就我個人而言,我發現將邏輯放在PHP中比在.htaccess中更容易,但這僅僅是個人的問題。 – GarethL
剛開始的時候,你所展示的代碼是相當危險且相當容易被利用的。 –