thinkphp URL规则、URL伪静态、URL路由、URL重写、URL生成(十五)
thinkphp URL规则、URL伪静态、URL路由、URL重写、URL生成(十五)
本章节:详细介绍thinkphp URL规则、URL伪静态、URL路由、URL重写、URL生成
一、URL规则
1、默认是区分大小写的
2、如果我们不想区分大小写可以改配置文件
'URL_CASE_INSENSITIVE'=>true,//url不区分大小写
*模块命名太长的情况:
A、如果模块名为 UserGroupAction,复杂模块(一般是IndexAction)
那么url找模块就必要要写成
http://localhost/thinkphp4/index.php/user_group/index
B、如果'URL_CASE_INSENSITIVE'=>false(区分大小写的情况可以访问)
那么url也可以写为
http://localhost/thinkphp4/index.php/UserGroup/index
二、URL伪静态(tp默认支持伪静态)
http://localhost/thinkphp4/index.php/UserGroup/index.xml
*默认pdo、html、xml...都是支持的,如果要限制加个配置就行
'URL_HTML_SUFFIX'=>'html|shtml|xml',//限制伪静态的后缀
三、URL路由
1、启动路由
要在配置文件中开启路由支持
'URL_ROUTER_ON' => true, //开启路由
2、使用路由
1.规则表达式配置路由
'URL_ROUTER_ON' => true, //开启路由
'URL_ROUTE_RULES' => array(
'my'=>'Index/index',//静态地址路由 访问:http://localhost/thinkphp/index.php/my
':id/:num'=>'Index/index',/*后面的数字值随便写,字母也行
动态地址路由 访问:http://localhost/thinkphp/index.php/10/200
可以再模块控制器中用get方式传值 或 获取
echo $_GET['id'];
echo $_GET['num'];
*/
'my/:num'=>'Index/index', //动静混合地址路由 http://localhost/thinkphp/index.php/my/200
'year/:year/:month/:date'=>'Index/index',//动态和静态混合地址路由: http://localhost/thinkphp/index.php/year/2014/12/21
'year/:year\d/:month\d/:date\d'=>'Index/index',//动态和静态混合地址路由 --加上 \d代表类型只能是数字
'my/:id$'=>'Index/index',// 加上$说明地址中只能是 my/1000 后面不能有其他内容了
);
2.正则表达式配置路由
//http://localhost/thinkphp/index.php/year/2014/12/21
'/^year\/(\d{4})\/(\d{2})\/(\d{2})/'=>'Index/index?year=:1&month=:2&date=:3'
3、注意事项:
1.越复杂的路由越往前面放
'URL_ROUTE_RULES'=>array(
'my/:year/:month:/:day'=>'Index/day', *复杂的路由放在前面,放后面就不会执行
'my/:id\d'=>'Index/index',
'my/:name'=>'Index/index',
)
2.可以使用$作为完全匹配的路由规则(不管复杂否,都会匹配所有正则)
'URL_ROUTE_RULES'=>array(
'my/:id\d$'=>'Index/index',
'my/:name$'=>'Index/index',
'my/:year/:month:/:day$'=>'Index/day',
),
3.用正则匹配的方式
'URL_ROUTE_RULES'=>array(
'/^my\/(\d+)$/'=>'Index/index?id=:1',
'/^my\/(\w+)$/'=>'Index/index?name=:1',
'/^my\/(\d{4})\/(\d{2})\/(\d{2})$/'=>'Index/day?year=:1&month=:2&day=:3',
),
四、URL重写
比如:http://localhost/thinkphp/index.php/Index/index.html/t/my ---- 不想让index.php出现
下面是Apache的配置过程,可以参考下:
1、httpd.conf配置文件中加载了mod_rewrite.so模块
2、AllowOverride None 将None改为 All
3、确保URL_MODEL设置为2 (该步骤省略)
4、把下面的内容保存为.htaccess文件放到入口文件的同级目录下
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php/$1 [QSA,PT,L]
重启Apache之后,原来的
就可以通过访问
http://localhost/thinkphp/Index/index.html/t/my --简化了URL地址,对SEO的支持度好点
五、URL生成(手册有详细介绍)
public function url(){
echo U('Index/add'); // 生成Index模块的add操作的URL地址
///thinkphp/index.php/index/add
}
上一篇http://qdxinbj8.2cto.com/index.php?m=content&c=content&a=public_preview&steps=1&catid=75&id=363637

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics





How to implement API routing in the Slim framework Slim is a lightweight PHP micro-framework that provides a simple and flexible way to build web applications. One of the main features is the implementation of API routing, allowing us to map different requests to corresponding handlers. This article will introduce how to implement API routing in the Slim framework and provide some code examples. First, we need to install the Slim framework. The latest version of Slim can be installed through Composer. Open a terminal and

Apache Camel is an Enterprise Service Bus (ESB)-based integration framework that can easily integrate disparate applications, services, and data sources to automate complex business processes. ApacheCamel uses route-based configuration to easily define and manage integration processes. Key features of ApacheCamel include: Flexibility: ApacheCamel can be easily integrated with a variety of applications, services, and data sources. It supports multiple protocols, including HTTP, JMS, SOAP, FTP, etc. Efficiency: ApacheCamel is very efficient, it can handle a large number of messages. It uses an asynchronous messaging mechanism, which improves performance. Expandable

In-depth analysis of the role and usage of the static keyword in C language. In C language, static is a very important keyword, which can be used in the definition of functions, variables and data types. Using the static keyword can change the link attributes, scope and life cycle of the object. Let’s analyze the role and usage of the static keyword in C language in detail. Static variables and functions: Variables defined using the static keyword inside a function are called static variables, which have a global life cycle

In C/C++, the pointer comparison rules are as follows: pointers pointing to the same object are equal. Pointers to different objects are not equal. Exception: Pointers to null addresses are equal.

How to use routing to customize page switching animation effects in a Vue project? Introduction: In the Vue project, routing is one of the functions we often use. Switching between pages can be achieved through routing, providing a good user experience. In order to make page switching more vivid, we can achieve it by customizing animation effects. This article will introduce how to use routing to customize the page switching animation effect in the Vue project. Create a Vue project First, we need to create a Vue project. You can use VueCLI to quickly build

How to use routing to implement page jump in Vue? With the continuous development of front-end development technology, Vue.js has become one of the most popular front-end frameworks. In Vue development, page jump is an essential part. Vue provides VueRouter to manage application routing, and seamless switching between pages can be achieved through routing. This article will introduce how to use routing to implement page jumps in Vue, with code examples. First, install the vue-router plugin in the Vue project.

In modern web applications, implementing web page navigation and routing is a very important part. Using JavaScript functions to implement this function can make our web applications more flexible, scalable and user-friendly. This article will introduce how to use JavaScript functions to implement web page navigation and routing, and provide specific code examples. Implementing web page navigation For a web application, web page navigation is the most frequently operated part by users. When a user clicks on the page

Implementation method and experience summary of flexible configuration of routing rules in PHP Introduction: In Web development, routing rules are a very important part, which determines the corresponding relationship between URL and specific PHP scripts. In the traditional development method, we usually configure various URL rules in the routing file, and then map the URL to the corresponding script path. However, as the complexity of the project increases and business requirements change, it will become very cumbersome and inflexible if each URL needs to be configured manually. So, how to implement in PHP
