php - Implementation of routing
仅有的幸福
仅有的幸福 2017-06-26 10:49:00
0
3
873

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!

仅有的幸福
仅有的幸福

reply all(3)
黄舟

For localhost/index.php, the accessed file address is localhost/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:

location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/run/php/php7.1-fpm.sock;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

        fastcgi_intercept_errors off;
        fastcgi_buffer_size 16k;
        fastcgi_buffers 4 16k;
        fastcgi_connect_timeout 300;
        fastcgi_send_timeout 300;
        fastcgi_read_timeout 300;
    }

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.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!