虽然这里几乎与我的问题太相关了,但我会作为我的"问题";和意图已经改变。
什么是";最好的";(准确、一致、有效(的方法来获得";可路由";URI的一部分。这里,我指的是URI中表示PHP应用程序的应用程序路由中要使用的虚拟目录的部分。例如:
引导物理路径:
c:/xampp/htdocs/path/to/app/index.php
Web根路径:c:/xampp/htdocs/
收到以下请求:
http://localhost/path/to/app/foo/bar/baz/
预期结果:
foo/bar/baz/
但
$_SERVER['REQUEST_URI']
包含:path/to/app/foo/bar/baz/
我过去常常把$_GET['_uri']
和mod_rewrite
结合使用,比如
# conditions
RewriteRule ^(.*)$ index.php?_uri=$1 [L,QSA]
但我现在切换到简单的:
# conditions
RewriteRule ^.*$ index.php [L,QSA]
到目前为止,已经有了这样的东西:
// get physical uri part, relative to doc root
$uriPhysical = trim(strtr(dirname($_SERVER['SCRIPT_NAME']), '''', '/'), '/') . '/';
// get full request uri, strip querystring
$uriLong = trim(strtr(substr($_SERVER['REQUEST_URI'], 0, strpos($_SERVER['REQUEST_URI'], '?')), '''', '/'), '/') . '/';
// chop the physical part off the beginning
$uriRoute = preg_replace('%^' . preg_quote($uriPhysical, '%') . '%i', '', $uriLong);
我是不是白跳了一圈?有更简单的方法吗?
您可以在解析中使用parse_url
。