0

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?

Community
  • 1
  • 1
Dan Lugg
  • 20,192
  • 19
  • 110
  • 174
  • What's so bad with substr? I assume you know the base URI, just cut it off from the start position where the baseuri ends. – hakre Jul 06 '11 at 15:38
  • @hakre - You mean in lieu of `preg_replace`? – Dan Lugg Jul 06 '11 at 15:44
  • No in lieu of the overall logic. An application should just know it's base URI and than cut it off from the (known) request URI. – hakre Jul 06 '11 at 16:52
  • @hakre - Understandably, I could do that, however I'm going for sort of a *CoC* approach to reduce any application configuration and maintain (*this part of*) the project's filesystem mobility. – Dan Lugg Jul 06 '11 at 16:57
  • Well, what is your convention to specify the base URI of the webroot then? By convention it's a file somewhere in the filesystem. Probably even a symlink. – hakre Jul 06 '11 at 17:04

1 Answers1

1

You could use parse_url in your parsing.

troelskn
  • 115,121
  • 27
  • 131
  • 155