Home  >  Article  >  Backend Development  >  Share the method of URL access in thinkPHP5.0 framework

Share the method of URL access in thinkPHP5.0 framework

黄舟
黄舟Original
2017-03-20 09:07:091653browse

这篇文章主要介绍了thinkPHP5.0框架URL访问方法,具体分析了thinkPHP5.0框架的URL路径结构与常见访问方法,以及隐藏入口文件的实现技巧,需要的朋友可以参考下

本文实例讲述了thinkPHP5.0框架URL访问方法。分享给大家供大家参考,具体如下:

URL设计

ThinkPHP5.0在没有启用路由的情况下典型的URL访问规则是:

http://serverName/index.php(或者其它应用入口文件)/模块/控制器/操作/[参数名/参数值...]

支持切换到命令行访问,如果切换到命令行模式下面的访问规则是:

>php.exe index.php(或者其它应用入口文件) 模块/控制器/操作/[参数名/参数值...]

可以看到,无论是URL访问还是命令行访问,都采用PATH_INFO访问地址,其中PATH_INFO的分隔符是可以设置的。

注意:5.0取消了URL模式的概念,并且普通模式的URL访问不再支持,如果不支持PATHINFO的服务器可以使用兼容模式访问如下:

http://serverName/index.php(或者其它应用入口文件)?s=/模块/控制器/操作/[参数名/参数值...]

必要的时候,我们可以通过某种方式,省略URL里面的模块和控制器。

URL大小写

默认情况下,URL是不区分大小写的,也就是说 URL里面的模块/控制器/操作名会自动转换为小写,控制器在最后调用的时候会转换为驼峰法处理。

例如:

http://localhost/index.php/Index/Blog/read
// 和下面的访问是等效的
http://localhost/index.php/index/blog/read

如果访问下面的地址

http://localhost/index.php/Index/BlogTest/read
// 和下面的访问是等效的
http://localhost/index.php/index/blogtest/read

在这种URL不区分大小写情况下,如果要访问驼峰法的控制器类,则需要使用:

http://localhost/index.php/Index/blog_test/read

如果希望URL访问严格区分大小写,可以在应用配置文件中设置:

// 关闭URL中控制器和操作名的自动转换
'url_convert'  => false,

一旦关闭自动转换,URL地址中的控制器名就变成大小写敏感了,例如前面的访问地址就要写成:

http://localhost/index.php/Index/BlogTest/read

但是下面的URL访问依然是有效的:

http://localhost/index.php/Index/blog_test/read

下面的URL访问则无效:

http://localhost/index.php/Index/BlogTest/read

需要注意:路由规则中定义的路由地址是按照控制器名的实际名称定义(区分大小写)。

隐藏入口文件

在ThinkPHP5.0中,出于优化的URL访问原则,还支持通过URL重写隐藏入口文件,下面以Apache为例说明隐藏应用入口文件index.php的设置。

下面是Apache的配置过程,可以参考下:

1、httpd.conf配置文件中加载了mod_rewrite.so模块
2、AllowOverride None 将None改为 All
3、在应用入口文件同级目录添加.htaccess文件
,内容如下:


Options +FollowSymlinks -Multiviews
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php/$1 [QSA,PT,L]

The above is the detailed content of Share the method of URL access in thinkPHP5.0 framework. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn