thinkphp底层原理速成:入口文件、路由模式、路由设置和url生成

张开发
2026/4/17 16:26:24 15 分钟阅读

分享文章

thinkphp底层原理速成:入口文件、路由模式、路由设置和url生成
本文详细介绍了ThinkPHP5.0的路由功能包括路由的作用、入口文件配置、路由模式普通、混合、强制、路由设置方法动态单个注册、动态批量注册、配置文件批量注册、变量规则、路由参数、资源路由的声明和自动注册规则以及快捷路由的声明和控制器使用。此外还讲解了如何生成URL以及隐藏入口文件的设置。一、路由的作用简化URL地址方便记忆有利于搜索引擎的优化二、入口文件前后台分离在网站public目录下项目\public新建admin.php打开admin.php1234567891011121314151617?php// ----------------------------------------------------------------------// | ThinkPHP [ WE CAN DO IT JUST THINK ]// ----------------------------------------------------------------------// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.// ----------------------------------------------------------------------// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )// ----------------------------------------------------------------------// | Author: liu21st liu21stgmail.com// ----------------------------------------------------------------------// [ 应用入口文件 ]// 定义应用目录define(APP_PATH, __DIR__ ./../application/);// 加载框架引导文件require__DIR__ ./../thinkphp/start.php;绑定模块实现功能index.php 这个入口文件只能去前台模块admin.php这个入口文件只能去后台模块建议后台入口文件复杂一些如何实现在入口文件中1234// 定义前台define(BIND_MODULE,index);// 绑定后台define(BIND_MODULE,admin);URL地址发生变化入口绑定之前http://www.tp.com/admin.php/模块/控制器/方法入口绑定之后http://www.tp.com/admin.php/控制器/方法隐藏入口文件开启apache重写D:\wamp64\bin\apache\apache2.4.23\conf\httpd.conf把注释开启 LoadModule rewrite_module modules/mod_rewrite.so设置访问权限D:\wamp64\bin\apache\apache2.4.23\conf\extra\httpd-vhosts.conf123456789VirtualHost *:80ServerName www.tp.comDocumentRoot D:/wamp64/www/study/thinkphpstudy/publicDirectoryD:/wamp64/www/study/thinkphpstudy/publicOptions Indexes Includes FollowSymLinks MultiViewsAllowOverride AllRequire all granted/Directory/VirtualHost入口文件在网站public目录下新建.htaccess文件12345678IfModule mod_rewrite.cOptions FollowSymlinks -MultiviewsRewriteEngine OnRewriteCond %{REQUEST_FILENAME} !-dRewriteCond %{REQUEST_FILENAME} !-fRewriteRule ^(.*)$ index.php/$1[QSA,PT,L]/IfModule重启服务url地址变化隐藏之前http://www.tp.com/index.php/控制器/方法隐藏之后http://www.tp.com/控制器/方法三、tp5.0路由学习注意支持三种方式的url解析规则路由只针对应用不针对模块因此路由的设置也是针对应用下的所有模块。关闭后台模块,在后台入口文件中admin.php写在加载框架引导文件之后否则报错。12345678910111213141516171819202122?php// ----------------------------------------------------------------------// | ThinkPHP [ WE CAN DO IT JUST THINK ]// ----------------------------------------------------------------------// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.// ----------------------------------------------------------------------// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )// ----------------------------------------------------------------------// | Author: liu21st liu21stgmail.com// ----------------------------------------------------------------------// [ 应用入口文件 ]// 定义应用目录define(APP_PATH, __DIR__ ./../application/);// 绑定后台define(BIND_MODULE,admin);// 加载框架引导文件require__DIR__ ./../thinkphp/start.php;// 关闭admin模块的路由\think\App::route(false);路由的三种模式普通模式1.定义关闭路由完全使用默认的PATH_INFO方式URL:2.形式http://www.tp.com/admin.php/index/index3.如何设置1234// 是否开启路由url_route_on false,// 是否强制使用路由url_route_must false,混合模式1.定义开启路由并使用路由定义默认PATH_INFO方式的混合2.如何设置1234// 是否开启路由url_route_on true,// 是否强制使用路由url_route_must false,强制模式1.定义开启路由并设置必需定义路由才能访问2.如何设置1234// 是否开启路由url_route_on true,// 是否强制使用路由url_route_must true,四、设置路由1.动态单个注册设置路由格式Route::rule(‘路由表达式’, ‘路由地址’, ‘请求类型’, ‘路由参数数组’, ‘变量规则数组’)设置路由文件项目\application\route.php如何设置(route.php)1234567usethink\Route;// 定义路由规则// 设置路由之后就不能使用pathinfo访问了Route::rule(/,index/index/index);//注册路由访问到index模块下的index控制器下的index的方法Route::rule(test,index/index/test);//注册路由test 访问到index模块下的index控制器下的test的方法路由的形式1、静态地址路由12//注册路由test 访问到index模块下的index控制器下的test的方法Route::rule(test,index/index/test);2、给路由带参数12345route.php中//注册带参数路由//http://www.tp.com/course/1//http://www.tp.com/index/index/index/id/1Route::rule(course/:id,index/index/course);1234index.php中functioncourse(){returninput(id);}3、给路由带多个参数设置了带几个就必需带几个12345route.php中//注册带参数路由//http://www.tp.com/time/1/2//http://www.tp.com/index/index/shijian/year/1/month/2Route::rule(time/:year/:month,index/index/shijian);1234index.php中functionshijian(){returninput(year).input(month);}4、可选参数路由12345route.php中//注册带可选参数路由//http://www.tp.com/time/1//http://www.tp.com/index/index/shijian/year/1Route::rule(time/:year/[:month],index/index/shijian);1234index.php中functionshijian(){returninput(year).input(month);}5、全动态路由不建议使用12345route.php中//注册带可选参数路由//http://www.tp.com/1/1//http://www.tp.com/index/index/dongtai/1/1Route::rule(:a/:b,index/index/dongtai);1234index.php中functiondongtai(){returninput(a).input(b);}6、完全匹配路由12345route.php中//注册带可选参数路由//http://www.tp.com/1/1//http://www.tp.com/index/index/dongtai/1/1Route::rule(:a/:b$,index/index/dongtai);1234index.php中functiondongtai(){returninput(a).input(b);}7、带额外参数123route.php中// 带额外参数Route::rule(test2,index/index/test2?id10nametian);1234index.php中functiontest2(){dump(input());}设置请求类型1.TP中请求类型get,post,put,delete2.Route::rule() 默认支持所有类型3.设置各种请求1234567891011121314151617// 支持get请求的两种方式Route::rule(type,index/index/type,get);Route::get(type,index/index/type);// 支持post请求的两种方式Route::rule(type,index/index/type,post);Route::post(type,index/index/type);// 同时支持get和postRoute::rule(type,index/index/type,get|post);// 支持所有路由Route::rule(type,index/index/type,*);Route::any(type,index/index/type);// 支持put请求Route::rule(type,index/index/type,put);Route::put(type,index/index/type);// 支持delete请求Route::rule(type,index/index/type,delete);Route::delete(type,index/index/type);4.如何模拟put和delete请求123456789form actiontypemethodpostpinput typehiddenname_methodvaluePUT/input typetextnamenameid//p pinput typesubmitvalue提交//p /form2.设置路由-动态批量注册1.基本格式12345Route::rule([路由规则1路由地址和参数,路由规则2[路由地址和参数,匹配参数数组,变量规则数组],...],,请求类型,匹配参数数组,变量规则);2.使用1234567891011// 动态批量注册Route::rule([indexindex/index/index,diaoyongindex/index/diaoyong,type/:idindex/index/type],,get);Route::get([indexindex/index/index,diaoyongindex/index/diaoyong,type/:idindex/index/type]);3.设置路由-配置文件批量注册12345return[indexindex/index/index,diaoyongindex/index/diaoyong,type/:idindex/index/type];五、变量规则Route::rule(‘路由表达式’,’路由地址’,’请求类型’,’路由参数数组’,’变量规则数组’);12// [id\d{1,3},name\w]设置路由变量规则id只能是1-3位数字name只能是hi字符串Route::rule(course/:id/:name,index/index/course,get,[],[id\d{1,3},name\w]);六、路由参数路由参数是指可以设置一些路由匹配的条件参数主要用于验证当前的路由规则是否有效主要包括12345678910111213141516171819Route::rule(course/:id/:name,index/index/course,get,[methodget,exthtml],[id\d{1,3},name\w]);// 路由参数method 请求方式必需是get// 路由参数ext 主要设置路由的后缀参数 说明method 请求类型检测支持多个请求类型ext URL后缀检测支持匹配多个后缀deny_ext URL禁止后缀检测支持匹配多个后缀https 检测是否https请求domain 域名检测before_behavior 前置行为检测after_behavior 后置行为执行callback 自定义检测方法merge_extra_vars 合并额外参数bind_model 绑定模型V5.0.1cache 请求缓存V5.0.1param_depr 路由参数分隔符V5.0.2ajax Ajax检测V5.0.2pjax Pjax检测V5.0.2七、资源路由1.声明Route::resource(‘blog’,’index/blog’);也可以在定义资源路由的时候限定执行的方法标识例如12Route::resource(blog,index/blog,[only[index,read,edit,update]]);Route::resource(blog,index/blog,[except[index,delete]]);2.会动注册7个路由规则一定要记忆标识请求类型生成路由规则对应操作方法默认indexGETblogindexcreateGETblog/createcreatesavePOSTblogsavereadGETblog/:idreadeditGETblog/:id/editeditupdatePUTblog/:idupdatedeleteDELETEblog/:iddelete八、快捷路由1.声明12// 声明快捷路由Route::controller(blog,index/blog);2.控制器123456classBlog{publicfunctiongeta(){echoaaaaaaaaa;}}3.url访问https://www.tp.com/blog/a 寻找geta方法https://www.tp.com/blog/index 寻找getindex方法九、url生成1.系统类1dump(Url::build(index/index/index));2.系统方法1dump(url(index/index/index));3.使用123456789101112131415161718192021222324252627282930publicfunctionindex(){echo我是blog控制器index方法;dump(Url::build(index/index/index));dump(url(index/index/index));dump(Url::build(index/index/test));dump(url(index/index/test));dump(Url::build(index/index/course/id/10));dump(url(index/index/course/id/10));//dump(Url::build(index/index/abc,[id10,name张三]));dump(url(index/index/abc,[id10,name张三]));dump(url(index/index/abc,id10name100));//带锚点dump(url(index/blog/read#name,id5));dump(url(index/blog/read#name,[id5,name100]));// 带域名dump(Url::build(index/blog/read#anchorblog,id5));dump(url(index/blog/read#anchorblog,[id5,name100]));http://blog.tp.com/blog/read/id/5/name/100.html#anchor// 加上入口文件Url::root(/index.php);dump(url(index/blog/read#anchorblog,[id5,name100]));//http://blog.tp.com/index.php/blog/read/id/5/name/100.html#anchor}复制讲解到此这篇关于thinkphp底层原理速成入口文件、路由模式、路由设置和url生成的文章就介绍到这了

更多文章