开发者

Obtaining the route-able URI part

While almost too related to my question here, I'll ask as my "question" and intent have changed.

What is the "best" (accurate, consistent, efficient) way to obtain the "route-able" part of a URI. By this, I mean the part of the URI representing the virtual directories to be used in the application routing of a PHP application. For example:

Bootstrap physical path:  c:/xampp/htdocs/path/to/app/index.php

Web root path:         c:/xampp/htdocs/

Given the following request:

http://localhost/path/to/app/foo/bar/baz/

Expected result:

foo/bar/baz/

...but $_SERVER['REQUEST_URI'] contains:

开发者_运维问答path/to/app/foo/bar/baz/

I used to use $_GET['_uri'] in conjunction with mod_rewrite like so:

# conditions
RewriteRule ^(.*)$ index.php?_uri=$1 [L,QSA]

But am now switching to simply:

# conditions
RewriteRule ^.*$ index.php [L,QSA]

And have got something like this so far:

// 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);

Have I jumped through a buncha hoops for nothing? Is there an easier way to do this?


You could use parse_url in your parsing.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜