search

Home  >  Q&A  >  body text

php - Implementation of routing

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!

仅有的幸福仅有的幸福2737 days ago934

reply all(3)I'll reply

  • 黄舟

    黄舟2017-06-26 10:51:00

    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().

    reply
    0
  • 淡淡烟草味

    淡淡烟草味2017-06-26 10:51:00

    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.

    reply
    0
  • 滿天的星座

    滿天的星座2017-06-26 10:51:00

    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.

    reply
    0
  • Cancelreply