URL操作

ThinkPHP 的 URL 操作。主要涉及到 URL 路径大小写、伪静态、生成以及模版中的 U()方法。

一、URL大小写

系统默认的规范是根据 URL 里面的模块名、控制器名来定位到具体的控制器类。比如:

1 http://localhost/demo39/index.php/Home/User/index/id/5

PS:在 windows 平台,URL 中的大小写会自动忽略,但作为开发人员,保持大小写区
分是一个良好的习惯。而 Linux 平台,如果大小写错误,会导致无法定位到指定的模块、
控制器。

在 WeiBo/Common/Conf/config.php 中进行配置:

1 //URL可以不区分大小写
2 'URL_CASE_INSENSITIVE' =>true

PS:如果开启了不区分大小写,那么对于  UserTypeController.class.php  这样的控
制器就需要如下访问方式:

1 http://localhost/demo39/index.php/home/user_type/index/

如果区分大小写,则是这样:

1 http://localhost/demo39/index.php/Home/UserType/index/

二、URL伪静态

URL 伪静态同城是为了满足更好的 SEO 效果,ThinkPHP 支持伪静态设置。

默认情况下,伪静态后缀为.html

1 http://localhost/demo39/User/index.html
2 http://localhost/demo39/User/index/id/5.html

设置伪静态后缀,默认为html

1 'URL_HTML_SUFFIX'=>'shtml',
1 http://localhost/demo39/User/index.shtml

如果设置为空,那么就任意后缀

1 'URL_HTML_SUFFIX'=>'',

如果设置为空,可以用__EXT__常量获取当前使用的后缀

在 View/User/index.html 中:

1 {$Think.const.__EXT__}

设置可以伪静态的后缀

1 'URL_HTML_SUFFIX'=>'html|shtml|xml',

禁止访问的后缀

1 'URL_DENY_SUFFIX' => 'html|pdf|ico|png|gif|jpg',

这时如果使用禁止访问的后缀来访问页面的话,页面无任何显示,在页面中查看源代码也无任何显示。

三、URL的生成

为了配合所使用的 URL 模式,我们需要能够动态的根据当前的 URL 设置生成对应的 URL
地址。为此,ThinkPHP 内置了一个 U()方法,用于 URL 的动态生成。

定义格式:U('地址表达式',['参数'],['伪静态后缀'],['显示域名'])

默认得到当前URL,在 Home/controller/UserController.class.php 中插入以下代码:

1 echo U();             ///demo39/User/index.html

 demo39 前面的三条 /// ,两条是注释符,一条是目录符。

地址表达式的格式如下:

[模块/控制器/操作#锚点@域名]?参数 1=值 1&参数 2=值 2...

控制器+方法

1 echo U('User/add');

输出为: /demo39/index.php/User/add.html ,需要注意的是 echo U('User/add'); 这里面的 User/add 跟它的文件名 UserController.class.php 中的 User 

没有任何关系,跟 class UserController extends Controller  也没有任何关系。

全部代码:

1 class UserController extends Controller {
2         public function index() {
3             echo u('Member/add');
4             $this->display();
5         }    
6 }

输出为: /demo39/index.php/Member/add.html 此时浏览器上的地址为: http://localhost/demo39/Home/User/index 

控制器+方法+?参数1=值1

1 echo U('User/add?id=5');

输出为: /demo39/index.php/User/add/id/5.html 

模块+控制器+方法+?参数1=值1

1 echo U('Admin/User/add?id=5');

输出为: /demo39/index.php/Admin/User/add/id/5.html 

U()方法第二个参数可以分离参数和值的操作,支持字符串和数组的写法。

使用数组参数1=值1,参数2=值2

1 echo U('User/add', array('id'=>5,'type'=>'a'));

输出为:  /demo39/index.php/User/add/id/5/type/a.html  

使用字符串参数1=值1,参数2=值2

1 echo U('User/add','id=5&type=a');

输出为: /demo39/index.php/User/add/id/5/type/a.html 

PS:不允许使用 PATHINFO 模式的路径来编写路径,比如:

1 echo U('User/add/id/1/type/a');

他会输出: /demo39/index.php/1/type/a.html ,但是这个生成的URL是错误的。

U()方法第三个参数可以指定伪静态后缀,比如:

1 //指定伪静态后缀
2 echo U('User/add', array('id'=>5),'xml');

输出为: /demo39/index.php/User/add/id/5.xml 

在 WeiBo/Common/Conf/config.php 设置 'URL_MODEL'=>0 ,普通模式后

1 echo U('User/add', array('id'=>5),'xml');

URL生成将如下: /demo39/index.php?m=&c=User&a=add&id=5 

设置 'URL_MODEL'=>1 ,PATHINFO模式后URL生成将如下:

输出为: /demo39/index.php/User/add/id/5.html 

设置 'URL_MODEL'=>2 ,REWRITE模式后URL生成将如下:

输出为: /demo39/User/add/id/5.html   

注意这时连 index.php 都省掉了。

设置 'URL_MODEL'=>3 ,兼容模式后URL生成将如下:

 输出为: /demo39/index.php?s=/User/add/id/5.html 

PS:在这里我们使用 REWRITE 重写模式,它是 PATHINFO 的升级版,地址最短。

我们也可以生成路由地址,使用路由的规则生成的地址:

在下面的数组里面写。

'URL_ROUTE_RULES' => array(
        ), 

(其实不在里面写的效果测试了是一样的,但还是服从规范吧)

在 WeiBo/Common/Conf/config.php 中:

1 //规则路由
2 'u/:idd'=>'User/index',

在 Home/controller/UserController.class.php 中:

1 //生成规则路由地址
2 echo U('/u/5');

输出为: /demo39/index.php/u/5.html 

生成正则路由地址

1 '/^u_(d+)$/'=>'User/index?id=:1',

在  Home/controller/UserController.class.php  中:

1 echo u('/u_5');

输出为: /demo39/index.php/u_5.html 

1 echo u('/u/5');

也会输出: /demo39/index.php/u/5.html 

域名支持:

1 echo U('User/add@www.ycuk.com?id=5');

则输出为: http://www.ycuk.com/demo39/index.php/User/add/id/5.html ,这时在 WeiBo/Common/Conf/config.php 不用其他配置。

add后面有@符号,不要忘了写

锚点支持

1 echo U('User/add#comment?id=5');

输出为: /demo39/index.php/User/add/id/5.html#comment 

四、模版中的U

我们之前使用 U()方法,都是在控制器里实现的。但实际用途中,大量的超链接是在模
版中设置的,而在控制器把 U()当变量传递过去,有点过于繁杂。所以,我们可以在模版使

用使用 U()方法。

1 在模版中使用U()方法
2 {:U('User/add', array('id'=>5))}

这时 Home/controller/UserController.class.php 中的代码为:

 1 <?php
 2 
 3 namespace HomeController;
 4 use ThinkController;
 5 
 6 class UserController extends Controller {
 7         public function index() {
 8             $this->display();
 9         }    
10 }

如果没有 $this->display(); ,则 View/User/index.html 里没有任何显示。

原文地址:https://www.cnblogs.com/jacson/p/4515209.html