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

SublimeText3 English version
Recommended: Win version, supports code prompts!

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

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

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Atom editor mac version download
The most popular open source editor