Routing: access the URL address of a specific method in the project. A simple understanding is the URL address
In ThinkPHP, the system provides 4 routing forms (previous routing Format: index.php?p=platform&c=controller&a=method; pca)
Summary: pca is platform, controller, method; mca in thinkPHP is group, controller, method
①Common form routing;
②Pathinfo form routing;
③Rewrite form routing;
④Compatible form routing
Detailed explanation below
(1) Ordinary form routing
Also called get form routing, all information is passed through get
Routing form: http://website/entry file (index.php)?m=group &c=controller name&a=method¶meter name=parameter value
For example: access the test method in the User controller under the Home group and pass a parameter id=1
http: //www.1336.com/index.php?m=Home&c=User&a=test&id=1
can be verified through $_GET['id'], output 1. So the verification is successful
Disadvantages: Everything passed by the url will be displayed, which is neither safe nor good-looking. So it’s not used much
(2) pathinfo() form routing (thinkPHP’s default routing)
Routing form: http://website/entry file (index.php)/group name /Controller name/Method/Parameter name 1/Parameter value 1/Parameter name n/Parameter value n
http://www.1336.com/index.php/Home/User/test/id/ 100
There is $_GET['id'] in the method, and 100 is output. So the verification is successful
(3)rewrite routing form (rewrite)
Routing form: http://URL/Group Name/Controller Name/Method Name/Parameter 1/Parameter Value 1 /Parameter n/Parameter value n
The difference from thinkPHP’s default routing form Pathinfo() routing is that the entry file is missing
Note: This routing form cannot be used directly. The configuration can only be used after the configuration is completed.
Configuration steps:
1. Configure httpd.conf under Apache and enable the extended rewrite rewrite module;
2. Modify the virtual host configuration file, The file is located in the Apache directory, conf/extra/httpd-vhosts, and allows configuration rewriting. Indicates that the current site is allowed to be rewritten
Simply put, add Allowoverride:all
#针对目录的详细配置#允许所有访问 allow from all #允许重写 AllowOverride all #表示允许站点显示文件目录结构 Options +indexes
3 to the directory configuration for the site that needs to be rewritten. Restart Apache
4. Copy the .htaccess file in the ThinkPHP compressed package to the same level directory as the entry file
After the configuration is completed, enter the URL for verification. The URL of the pathinfo above is also used here. Remove the entry file and refresh it. Found Output 100. OK~Configuration completed
Expansion:
PHP operating mode (five major operating modes):
CGI mode (universal gateway interface);
FastCGI Mode (resident CGI);
CLI (command line operation);
Web module mode (mode for running Web servers such as Apache);
ISAPI mode (5.3 No longer supported after this version)
The mode of the environment integration package currently used is the Web module mode, and the FastCGI resident mode is commonly used in work development.
To sum up, rewrite mode requires Apache support and is generally not recommended. In addition to Apache, server software also includes nginx, lightd, etc.
(4) Compatible routing form
The compatible routing form combines the features of ordinary routing form, pathinfo routing form, and rewrite routing form
Routing form: http://website/ Entry file?s=/group name/controller name/method name/parameter 1/parameter value 1
Note: The compatible routing form has only one parameter: parameter name s, which can be regarded as s parameter value later
For example: http://www.1336.com/index.php?s=/Home/User/test/id/100, the verification output is 100, indicating that the access is successful
(5) Extension: Configuration of routing form in ThinkPHP
Configuration file ThinkPHP/Conf/convention.php, ThinkPHP conventional configuration file
/* URL设置 */ 'URL_CASE_INSENSITIVE' => true, // 默认false 表示URL区分大小写 true则表示不区分大小写 'URL_MODEL' => 1,
URL access mode, optional parameters 0, 1, 2, 3, Represents the following four modes:
0 (normal mode);
1 (Pathinfo mode);
2 (REWRITE mode);
3 ( Compatibility mode) The default is Pathinfo mode
Note: The configuration value of the routing form does not affect other forms of routing access entered in the address bar. The value of this configuration item affects the form of the URL address generated by the URL assembly function (U function) encapsulated by the ThinkPHP system
ThinkPHP supports four URL modes, which can be defined by setting the URL_MODEL parameter, including normal mode, PATHINFO, REWRITE and compatibility mode.
1. Normal mode: 'URL_MODEL'=>0,
http://serverName/appName/?m=module&a=action&id=1
2. PATHINFO mode: 'URL_MODEL'=>1, (system default mode)
The URL_PATHINFO mode is used by default. PATHINFO mode also includes normal mode and smart mode:
PATHINFO normal mode: 'PATH_MODEL'=>1,
There is no order for URL parameters in this mode, for example
http://serverName/appName/m/module/a/action/id/1 http://serverName/appName/a/action/id/1/m/module
PATHINFO smart mode:'PATH_MODEL'=>2, (system default mode)
This mode automatically identifies modules and operations, such as
http://serverName/appName/module/action/id/1/ http://serverName/appName/module,action,id,1/
In smart mode , the first parameter will be parsed into the module name (or route name, described below), the second parameter will be parsed into the operation (under the premise that the first parameter is not the route name), and the following parameters are explicit are passed by formula and must appear in pairs, for example:
http://serverName/appName/module/action/year/2000/month/01/day/01/
其中参数之间的分割符由PATH_DEPR参数设置,默认为"/",若设置PATH_DEPR为"^",则:
http://serverName/appName/module^action^id^1/
注意不要使用"@" 和"&"符号进行分割,该符号有特殊用途,可能会导致其他的冲突。
如果想要简化URL的形式可以通过路由功能(后面会有描述),在PATHINFO模式下,会把相关参数转换成GET变量,以及并入REQUEST变量,因此不妨碍应用里面的以上变量获取。
3、REWRITE模式:'URL_MODEL'=>2,
该URL模式和PATHINFO模式功能一样,除了可以不需要在URL里面写入口文件,和可以定义.htaccess 文件外。
例如,我们可以增加如下的.htaccess内容把所有操作都指向index.php文件。
RewriteEngine on RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^(.*)$ index.php/$1 [QSA,PT,L]
4、兼容模式:'URL_MODEL'=>3
兼容模式是普通模式和PATHINFO模式的结合,并且可以让应用在需要的时候直接切换到PATHINFO模式而不需要更改模板和程序,基本上可以支持任何的运行环境。
只需要传入PATHINFO 兼容模式获取变量VAR_PATHINFO,默认值为s,例如
http://serverName/appName/?s=/module/action/id/1/
会执行和上面的URL等效的操作,并且也可以支持参数分割符号的定义,例如在PATH_DEPR设置为"~"的情况下,下面的URL有效:
http://serverName/appName/?s=module~action~id~1
兼容模式的情况下面模板文件不用做任何更改,保持和PATHINFO模式的写法一样,只需要在切换URL模式的时候清空下模板缓存目录。
推荐教程:《TP5》
The above is the detailed content of Introduction to thinkphp's four url modes. For more information, please follow other related articles on the PHP Chinese website!