這取決於獲取一個值。如果您使用的是Apache,您可以使用.htaccess
來重寫您的網址並將它們指向PHP腳本。
RewriteEngine on
RewriteCond $1 !^(index\.php|robots\.txt|)
RewriteRule ^(.*)$ /index.php/$1 [L]
這將需要一個url像example.com/eventshow/1234
,重定向到您的index.php,然後服務器變量$_SERVER['PATH_INFO']
設置爲eventshow/1234
。從那裏你可以解析PATH_INFO來確定應該調用哪個函數以及應該傳遞哪些值。 $_SERVER['PATH_INFO']
最大的問題是它是通過無用的斜槓/
所以你想確保修剪它們。
在您的例子我會寫這樣的:
的index.php
if(!empty(trim($_SERVER['PATH_INFO'],"/"))){
//clean extraneous slashes and explode
$request = array_filter(explode("/",trim($_SERVER['PATH_INFO'],"/"));
}else{
//if no entity specified call index entity, I put this in to handle example.com/ requests.
$request = array("Index");
}
$func = array_shift($request);
$result = call_user_func_array($func, $request);
給出的URL example.com/eventshow/1234
最後一行將調用相當於:
$result = eventshow(1234);
您可能還希望在調用$ func之前做一些理智的檢查,檢查函數是否存在,添加權限檢查以及輸入過濾器。
您肯定有語法錯誤,請檢查您的引號。 – bobkingof12vs