Home > Backend Development > PHP Tutorial > Let's talk about PHP routing and pseudo-static applications

Let's talk about PHP routing and pseudo-static applications

WBOY
Release: 2023-04-11 09:40:01
forward
4182 people have browsed it

This article brings you relevant knowledge about PHP, which mainly introduces the relevant content about routing and pseudo-static applications. Routing refers to line distribution, and pseudo-static is to make dynamic URL addresses look static. Let’s take a look at the address below. I hope it will be helpful to everyone.

Let's talk about PHP routing and pseudo-static applications

Route

Literally explained, road is a line, road; by, reason, meaning , represents rules; so it means where it is assigned according to what rules, or meanings.

So routing refers to line distribution. In the back-end web, it generally refers to the mapping rules for accessing URLs, such as what kind of URL access is mapped to what module and what controller through routing rules. Some method, or a class, or a closure, or something else, in short it plays the role of distribution.

Pseudo-static

Pseudo-static literally means fake, static.

(Supplement: Pseudo-static is to make the dynamic URL address look like a static address. Pseudo-static is the ultimate goal, not the technology. There are many ways to achieve pseudo-static. This can be achieved by using the URL rewriting of the host, or by using the routing configuration of the program itself. For example, URL rules can be freely configured in tp5 and wordpress. The former requires permission to manage the host, while the latter is more flexible. However, pathinfo is required to hide the entrance. Or url rewriting support)

In the world of web2.0, many pages are dynamic, such as many index.php/a/... or index.php?a=1&... In this way, such dynamic addresses are not very good-looking and not friendly to search engines. Some technical means are needed to disguise these dynamic URL addresses into static URL addresses. This is pseudo-static.

So pseudo-static refers to a solution that uses some technologies to convert dynamic addresses into static addresses and can be accessed as usual. It does not refer to a certain technology.

So let’s take a look at what technologies are needed to achieve pseudo-static:

  • web host rewrite module (URL rewriting, rewrite translated into Chinese For rewriting, but many people also often call it pseudo-static)

  • pathinfo (a technology supported by the host, index.php/path, and then reuse the url Writing can hide the entrance)

  • routing of the program (can beautify, shorten the url, become more humane and semantic)

Supplement: There is another very important point that has not been mentioned, which is [Add suffix]. This is not considered a technology, so it is not listed in the above technology list. Adding suffix can be used in each step above. It can be done. In the program, you just need to control the suffix when generating the URL. And suffixes are sometimes useful when designing APIs. The suffix must represent the returned data format (reference: REST design style).

Analysis:

  • #The first solution has the advantage of being powerful (using rewriting module rules to support regular matching and other features), Arbitrary url rewriting can be achieved, but the disadvantage is that many complex url rewriting rules need to be configured (the Duoduo rebate program uses this method to make pseudo-static. If pseudo-static is turned on, the program will generate URLs with pseudo-static rules and requires a host. At the same time, manually configure the corresponding URL rewriting rules), you need to have host management permissions. If the configuration is modified on the host, you need to restart the web server (supporting .htaccess does not require restarting).

  • This requires the support of the web host. The host that supports PATHINFO can support url access such as index.php/home/index/index, so that the value can be obtained in the web language (such as PHP The program can get $_SERVER['PATH_INFO'] = '/home/index/index'). According to this feature, Using URL rewriting, only a simple URL rewriting rule is needed to rewrite all parameters after the entry file, so that the entry file can be hidden. Of course, this is just hiding Entry file, in thinkphp, if the host does not support PATHINFO (such as a lower version of nginx), then using the compatibility mode (?s=/m/c/a) can also simulate pathinfo to hide the entry file index.php

  • It can be said that it is the url rewriting in the program (the rewrite mentioned above is the URL rewriting of the host). The design refers to the url rewriting of the host, so the routing rules are also the same as the host url rewriting rules. almost. The advantages are powerful functions and flexible rule configuration; the disadvantage is the performance of parsing (this is the only disadvantage, the others are advantages). (WordPress custom url is actually a custom route)

Summary:

一般主机都能开启rewrite模块/功能支持URL重写,这很关键,这样实现隐藏入口文件了,这是第一步,再利用pathinfo(或者兼容模式),还有程序的路由,这样配合起来就能够实现简洁明了的URL地址了,也就是实现我们所说的伪静态。

扩展知识

原始的 index.php?m=home&c=index&a=index 实在是太难看了,所以即使不全伪静态,不用路由,也最好抛弃这种原始的地址,因为有些接口开发中已经不支持使用这类url地址了,所以务必使用 pathinfo 的形式:index.php/home/index/index,在隐藏入口,再加个后缀 home/index/index.html 就完美了,如果是做API或者有特殊用途的话还可以带上特定的后缀,比如看云、github仓库克隆时代.git后缀的仓库地址。

其实如果你的入口是index.php,使用兼容模式也可以隐藏入口的,直接?s=/m/c/a,?m=home&c=index&a=index 因为大多主机默认配置的有默认访问文件/入口。

下面分析nginx的部分配置:

主配置:/usr/local/nginx/conf/nginx.conf

user www www;worker_processes auto;error_log /data/wwwlogs/error_nginx.log crit;pid /var/run/nginx.pid;worker_rlimit_nofile 51200;events {
    use epoll;
    worker_connections 51200;
    multi_accept on;
    }

http {
    include mime.types;
    default_type application/octet-stream;
    server_names_hash_bucket_size 128;
    client_header_buffer_size 32k;
    large_client_header_buffers 4 32k;
    client_max_body_size 1024m;
    sendfile on;
    tcp_nopush on;
    keepalive_timeout 120;
    server_tokens off;
    tcp_nodelay on;

    fastcgi_connect_timeout 300;
    fastcgi_send_timeout 300;
    fastcgi_read_timeout 300;
    fastcgi_buffer_size 64k;
    fastcgi_buffers 4 64k;
    fastcgi_busy_buffers_size 128k;
    fastcgi_temp_file_write_size 128k;

    #Gzip Compression
    gzip on;
    gzip_buffers 16 8k;
    gzip_comp_level 6;
    gzip_http_version 1.1;
    gzip_min_length 256;
    gzip_proxied any;
    gzip_vary on;
    gzip_types
        text/xml application/xml application/atom+xml application/rss+xml application/xhtml+xml image/svg+xml
        text/javascript application/javascript application/x-javascript
        text/x-json application/json application/x-web-app-manifest+json
        text/css text/plain text/x-component
        font/opentype application/x-font-ttf application/vnd.ms-fontobject
        image/x-icon;
    gzip_disable "MSIE [1-6].(?!.*SV1)";

    #If you have a lot of static files to serve through Nginx then caching of the files' metadata (not the actual files' contents) can save some latency.
    open_file_cache max=1000 inactive=20s;
    open_file_cache_valid 30s;
    open_file_cache_min_uses 2;
    open_file_cache_errors on;######################## default ############################
    server {
    listen 80;
    server_name _;
    access_log /data/wwwlogs/access_nginx.log combined;
    root /data/wwwroot/default;
    index index.html index.htm index.php;
    location /nginx_status {
        stub_status on;
        access_log off;
        allow 127.0.0.1;
        deny all;
        }
    location ~ [^/].php(/|$) {        #fastcgi_pass remote_php_ip:9000;
        fastcgi_pass unix:/dev/shm/php-cgi.sock;
        fastcgi_index index.php;
        include fastcgi.conf;
        }
    location ~ .*.(gif|jpg|jpeg|png|bmp|swf|flv|ico)$ {
        expires 30d;
        access_log off;
        }
    location ~ .*.(js|css)?$ {
        expires 7d;
        access_log off;
        }
    }########################## vhost #############################
    include vhost/*.conf;}
Copy after login

每一个虚拟主机在虚拟主机配置目录都有一个对应的配置文件

[root@iZuf6fvttmu9vkdbnencgpZ vhost]# lsdingtalk.uogo8.com.conf  lipin.uogo8.com.conf  tv.uogo8.com.conf
gj.uogo8.com.conf        test.uogo8.com.conf   www.uogo8.com.conf
Copy after login

虚拟主机配置文件:/usr/local/nginx/conf/vhost/www.uogo8.com.conf

server {
listen 80;
server_name www.uogo8.com uogo8.com;
access_log /data/wwwlogs/www.uogo8.com_nginx.log combined;
index index.html index.htm index.php;
include /usr/local/nginx/conf/rewrite/www.uogo8.com.conf;
root /data/wwwroot/www.uogo8.com;if ($host != www.uogo8.com) {
    rewrite ^/(.*)$ $scheme://www.uogo8.com/$1 permanent;
    }

location ~ .php {    #fastcgi_pass remote_php_ip:9000;
    fastcgi_pass unix:/dev/shm/php-cgi.sock;
    fastcgi_index index.php;
    include fastcgi_params;    set $real_script_name $fastcgi_script_name;        if ($fastcgi_script_name ~ "^(.+?.php)(/.+)$") {        set $real_script_name $1;        #set $path_info $2;
        }
    fastcgi_param SCRIPT_FILENAME $document_root$real_script_name;
    fastcgi_param SCRIPT_NAME $real_script_name;    #fastcgi_param PATH_INFO $path_info;
    }
location ~ .*.(gif|jpg|jpeg|png|bmp|swf|flv|ico)$ {
    expires 30d;
    access_log off;
    }
location ~ .*.(js|css)?$ {
    expires 7d;
    access_log off;
    }
}
Copy after login

伪静态单独放一个目录,需要时被虚拟主机配置文件引入,这样方便管理

[root@iZuf6fvttmu9vkdbnencgpZ vhost]# cd ../rewrite/[root@iZuf6fvttmu9vkdbnencgpZ rewrite]# lsnone.conf  thinkphp.conf  www.uogo8.com.conf
Copy after login

伪静态是一种效果,准确的说这是主机的URL重写规则/配置文件。但一般大家也习惯性称之为伪静态配置、伪静态文件,开启伪静态,伪静态模块,伪静态功能了,其实伪静态只是最终实现的效果。

thinkphp程序的伪静态配置文件:/usr/local/nginx/conf/rewrite/thinkphp.conf

location / {
    if (!-e $request_filename) {        # 不支持pathinfo,那就使用url重写,重写成兼容模式,来达到隐藏入口的效果
        rewrite ^(.*)$ /index.php?s=$1 last;
        break;
    }
}
Copy after login

thinkphp伪静态配置文件.htaccess(Apache环境)

<IfModule mod_rewrite.c>
  Options +FollowSymlinks -Multiviews
  RewriteEngine On

  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{REQUEST_FILENAME} !-f  # 直接利用url重写,重写成 pathinfo 模式
  RewriteRule ^(.*)$ index.php/$1 [QSA,PT,L]
</IfModule>
Copy after login

uogo8的伪静态配置文件:/usr/local/nginx/conf/rewrite/www.uogo8.com.conf

location / {    if (!-e $request_filename) {
        rewrite  ^/(.*)$  /index.php/$1  last;        break;
    }
}
Copy after login
########################################################################       OneinStack for CentOS/RadHat 5+ Debian 6+ and Ubuntu 12+      ##       For more information please visit https://oneinstack.com      ########################################################################Your domain:                  www.uogo8.com
Virtualhost conf:             /usr/local/nginx/conf/vhost/www.uogo8.com.conf
Directory of:                 /data/wwwroot/www.uogo8.com
Rewrite rule:                 /usr/local/nginx/conf/rewrite/thinkphp.conf
Copy after login

推荐学习:《PHP视频教程

The above is the detailed content of Let's talk about PHP routing and pseudo-static applications. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:juejin.im
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template