search
HomePHP FrameworkThinkPHPIntroduction to thinkphp's four url modes

Introduction to thinkphp's four url modes

May 05, 2020 am 09:10 AM
thinkphp

Introduction to thinkphp's four url modes

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

#针对目录的详细配置
    <Directory>
        #允许所有访问
        allow from all
        #允许重写
        AllowOverride all
        #表示允许站点显示文件目录结构
        Options +indexes
    </Directory>

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设置 */
&#39;URL_CASE_INSENSITIVE&#39;  =>  true,   // 默认false 表示URL区分大小写 true则表示不区分大小写
&#39;URL_MODEL&#39;             =>  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!

Statement
This article is reproduced at:博客园. If there is any infringement, please contact admin@php.cn delete
What Are the Key Features of ThinkPHP's Built-in Testing Framework?What Are the Key Features of ThinkPHP's Built-in Testing Framework?Mar 18, 2025 pm 05:01 PM

The article discusses ThinkPHP's built-in testing framework, highlighting its key features like unit and integration testing, and how it enhances application reliability through early bug detection and improved code quality.

How to Use ThinkPHP for Building Real-Time Stock Market Data Feeds?How to Use ThinkPHP for Building Real-Time Stock Market Data Feeds?Mar 18, 2025 pm 04:57 PM

Article discusses using ThinkPHP for real-time stock market data feeds, focusing on setup, data accuracy, optimization, and security measures.

What Are the Key Considerations for Using ThinkPHP in a Serverless Architecture?What Are the Key Considerations for Using ThinkPHP in a Serverless Architecture?Mar 18, 2025 pm 04:54 PM

The article discusses key considerations for using ThinkPHP in serverless architectures, focusing on performance optimization, stateless design, and security. It highlights benefits like cost efficiency and scalability, but also addresses challenges

How to Implement Service Discovery and Load Balancing in ThinkPHP Microservices?How to Implement Service Discovery and Load Balancing in ThinkPHP Microservices?Mar 18, 2025 pm 04:51 PM

The article discusses implementing service discovery and load balancing in ThinkPHP microservices, focusing on setup, best practices, integration methods, and recommended tools.[159 characters]

What Are the Advanced Features of ThinkPHP's Dependency Injection Container?What Are the Advanced Features of ThinkPHP's Dependency Injection Container?Mar 18, 2025 pm 04:50 PM

ThinkPHP's IoC container offers advanced features like lazy loading, contextual binding, and method injection for efficient dependency management in PHP apps.Character count: 159

How to Use ThinkPHP for Building Real-Time Collaboration Tools?How to Use ThinkPHP for Building Real-Time Collaboration Tools?Mar 18, 2025 pm 04:49 PM

The article discusses using ThinkPHP to build real-time collaboration tools, focusing on setup, WebSocket integration, and security best practices.

What Are the Key Benefits of Using ThinkPHP for Building SaaS Applications?What Are the Key Benefits of Using ThinkPHP for Building SaaS Applications?Mar 18, 2025 pm 04:46 PM

ThinkPHP benefits SaaS apps with its lightweight design, MVC architecture, and extensibility. It enhances scalability, speeds development, and improves security through various features.

How to Build a Distributed Task Queue System with ThinkPHP and RabbitMQ?How to Build a Distributed Task Queue System with ThinkPHP and RabbitMQ?Mar 18, 2025 pm 04:45 PM

The article outlines building a distributed task queue system using ThinkPHP and RabbitMQ, focusing on installation, configuration, task management, and scalability. Key issues include ensuring high availability, avoiding common pitfalls like imprope

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use