Routing page requests with PHP the right way

2019-02-15 05:47发布

I need to know the term and best practices of performing site navigation the "right"? way, similar to how stackoverflow routes you when you ask a question via the url:

"http://stackoverflow.com/questions/ask"

Where as with my knowledge of PHP programming I would probably code it like so:

"http://stackoverflow.com/index.php?p=questions&act=ask"

Hopefully you understand what I mean. I would like to know the term for this method of page navigation and request/response handling, and if possible the best practices, limitations, or anything else I need to keep in mind when designing a web application using this standard / method. I also don't even know if this is all done with PHP or some web backend coded in ASP or Ruby or what have you, so I have populated the tags with my guesses.

3条回答
啃猪蹄的小仙女
2楼-- · 2019-02-15 06:04

They are most likely using the same method you are describing (embedding the navigation variables) within the URL, but it is being done "under the hood".

The mechanism that allows you to present URLs such as this is called MOD Rewrite. It uses the combination of the variables in the URL, and regular expressions to re-represent the URL to the end-user in a more user-friendly manner.

More Information: http://httpd.apache.org/docs/current/mod/mod_rewrite.html

Edit: Of course this would apply to code running on apache webserver. There are probably similar modules for other web servers such as IIS.

Also, mind you that mod_rewrite is outside of the scope of php. It is instead actually apache server directives, which are invoked before php even comes into play.

查看更多
劫难
3楼-- · 2019-02-15 06:10

I am currently developing a php router which is targeted at extreme high performance. you probably might want to take a look:

https://github.com/c9s/Pux

Pux is 48.5x faster than symfony router in static route dispatching, 31x faster in regular expression dispatching. (with pux extension installed)

Pux tries not to consume computation time to build all routes dynamically (like Symfony/Routing). Instead, Pux compiles your routes to plain PHP array for caching, the compiled routes can be loaded from cache very fast.

With Pux PHP Extension support, you may load and dispatch the routes 1.5~2x faster than pure PHP Pux.

查看更多
倾城 Initia
4楼-- · 2019-02-15 06:15

The pattern that most MVC frameworks use is a front controller that calls on a router. The front controller is typically an index.php in your web root. Next, all requests that aren't for existing files (like js, css, and image assets) need to be sent to this controller. In apache, you can do this with mod_rewrite:

RewriteRule ^index\.php$ - [F]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . /index.php [L,QSA]

However, the recommended way in Apache 2.5 is with FallbackResource:

FallbackResource index.php

IIS has similar functionality if that's what you're using.

In index.php, you can access the URL originally requested with $_SERVER['REQUEST_URI']. You should include your router (which should be outside of the web root) and invoke it with the request URI. Example:

require '../router.php';

$router = new Router();
$router->process($_SERVER['HTTP_METHOD'], $_SERVER['REQUEST_URI'], $_GET, $_POST);

Then your router can find the appropriate controller to route the request to. Read more on the MVC framework, and study some examples to better understand how others have implemented it.

查看更多
登录 后发表回答