0

For my own MVC I need to read the request URI from the global variables ($_GET or $_SERVER).

First I thought to read it from $_GET array. But then I discovered that it's contained in the $_SERVER array as well.

So, I would like to ask, from which global array should the request URI be read?


An example:

The URI could have the following structure:

http://local.mvc/PsrTest/testRequest/123?var=someval

with:

  • PsrTest as controller name;
  • testRequest as action name;
  • 123 as argument for the controller action;
  • var=someval as some query string key/value pair;

By applying a RewriteRule in ".htaccess", it will be translated to:

http://local.mvc/index.php?url=PsrTest/testRequest/123&var=someval

and it will be saved in the following items of the $_GET and $_SERVER arrays:

------------
$_GET array:
------------

'url' => 'PsrTest/testRequest/123'
'var' => 'someval'

---------------
$_SERVER array:
---------------

'HTTP_REFERER' => 'http://local.mvc/PsrTest/testRequest/123?var=someval'
'REDIRECT_QUERY_STRING' => 'url=PsrTest%2ftestRequest%2f123&var=someval'
'REDIRECT_URL' => '/PsrTest/testRequest/123'
'QUERY_STRING' => 'url=PsrTest%2ftestRequest%2f123&var=someval'
'REQUEST_URI' => '/PsrTest/testRequest/123?var=someval'

Thank you for your time!

tereško
  • 58,060
  • 25
  • 98
  • 150
  • 1
    Don't. You should leave the routing to PHP. It's not like all webservers have .htaccess file (like nginx and lighttpd). – tereško Nov 02 '17 at 10:56
  • 1
    I agree with this. Having used .htaccess to use this method using annotation is the best way forward. Symfony MVC does this very well. If you are building your own MVC you should be able to take part of this code and use it in your own. https://symfony.com/doc/current/routing.html – Robert Saylor Nov 02 '17 at 11:16
  • Hi, @tereško and thanks! You mean, I shouldn't apply a `RewriteRule`? Or something else? Could you please explain it a bit, so that I understand the idea behind your advice? Actually I would much appreciate an answer where you can put more words, so to say. For my MVC I'm in the point where I need to build the requested URI and to implement `ServerRequestInterface` (PSR-7) with it and to call the controller action with it. –  Nov 02 '17 at 11:21
  • 1
    I would just write these lines in `.htaccess: RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^ index.php [L]` – tereško Nov 02 '17 at 11:54
  • 1
    And then use some routing library in PHP to split the `$_SERVER['REQUEST_URI']` in parameters – tereško Nov 02 '17 at 11:55
  • @tereško Oh, now I understand what you mean. So I can now say: indeed the possibility of absence of `.htaccess` is a very good argument to not trying to pass the requested URI to a query string key. I'm very enthusiastic right now, because all is clear now. Thanks to you! :-) Have a nice day and good luck. –  Nov 02 '17 at 12:32
  • @tereško P.S: Exactly at the point of implementing the splitting of `$_SERVER['REQUEST_URI']` in parameters I am right now. –  Nov 02 '17 at 12:35

2 Answers2

0

But if you want to use .htaccess this is a start.

RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f

# One param
RewriteRule ^/?([^/]*)$ index.php?section=$1 [NC,L]

# Two params
# IE: search/12345
RewriteRule ^edituser/([a-zA-Z0-9]+)/?$ index.php?section=edituser&id=$1 [NC,L]

# Three params
# IE: search/12345/676767
RewriteRule ^editcontact/([a-zA-Z0-9]+)/([a-zA-Z0-9]+)/?$ index.php?section=editcontact&contactID=$1&reservationID=$2 [NC,L]

This is a small example from an older program I wrote.

Robert Saylor
  • 1,279
  • 9
  • 11
  • Thank you very much, @RobertSaylor. Unfortunately I'm not sure what you mean (in your comment neither), because I think you and teresko are referring yourselfs to something that I don't... catch :-) Theoretically I have a similar code as yours: `RewriteRule ^(.+)$ index.php?url=$1 [QSA,L,B]`. And I can read and parse all that key/value pairs from the `url`. But, for example, I need to also get the URI scheme, host, port, etc. –  Nov 02 '17 at 11:33
  • For my MVC I'm in the point where I need to build the requested URI, to implement `UriInterface` (PSR-7) with it, then to implement `ServerRequestInterface` (PSR-7) and to call the controller action, passing the `ServerRequest` instance as argument. –  Nov 02 '17 at 11:36
  • Now it is all clear for me. Teresko suggested that `.htaccess` are not always a possibility. And I think you too, but I didn't understood first what you both meant :-) So it makes no more sense for me to write `RewriteRule`s which translate the given uri into query string parameters in `.htaccess` or virtual host. Therefore I'll delegate the rewrite rule responsibilities to my PHP code. What you actually provided in your answer I will program in PHP, so to say. But I want to thank you again for your answer and effort! :-) Good luck. –  Nov 02 '17 at 12:47
0

As @teresko suggested, .htaccess are not always a possibility. Then it makes indeed no more sense (at least for me) to write RewriteRules in .htaccess, which translate the given URI into query string parameters. It will be sufficient to:

  • Just write a simple rule, like RewriteRule ^ index.php [L] in .htaccess;
  • Just parse the $_SERVER['REQUEST_URI'] value in PHP, in order to get the URI components needed to call a controller action and to pass the corresponding parameters to it.

I'm grateful to all the users who helped me finding my answer. Thanks!