When using website software, I found that the index.php in the root directory of some software can generate multiple different web pages, such as:
localhost/index.php/blog; localhost/index.php/contact.
How is this routing method implemented? Thanks!
For
localhost/index.php
, the accessed file address islocalhost/index.php
That’s right, and then/blog
can be understood as a parameter,How to get it? Check the information of $_SERVER.
For example, the '/blog' parameter corresponds to the index method of the Blog controller, and then the corresponding logic can be implemented by calling (new Blog())->index().
Going on, the '/blog/add' parameter corresponds to the add method of the Blog controller, and then the corresponding logic can be implemented by calling (new Blog())->index().
For the server, www.xxx.com/index.php/test can only recognize www.xxx.com/index.php. This is configured in Nginx or Apche, such as the following configuration:
As for the subsequent information test, it is recognized by the PHP code itself. It is actually a parameter. (As for how the PHP code receives this parameter, you have to see how Nginx or Apache interacts with PHP.) When this After getting the parameter "test", you can do what you want based on this parameter. With different parameters, you can do different things and provide a variety of functions. This is also my understanding of routing.
The following parameter format is called PATH_INFO, apache prompts by default, nginx needs to match it by itself.
This is the same as index.php?url=/path/to/xxx. It just uses a parameter to represent the route of the project.