龙少华 發表於 2026-5-3 17:22:46

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

<div id="navCategory"><h5 class="catalogue">目录</h5><ul class="first_class_ul"><li><a href="#_label0">一、路由的作用</a></li><li><a href="#_label1">二、入口文件</a></li><ul class="second_class_ul"><li><a href="#_lab2_1_0">前后台分离</a></li><li><a href="#_lab2_1_1">绑定模块</a></li><li><a href="#_lab2_1_2">隐藏入口文件</a></li></ul><li><a href="#_label2">三、tp5.0路由学习注意</a></li><ul class="second_class_ul"><li><a href="#_lab2_2_3">路由的三种模式</a></li></ul><li><a href="#_label3">四、设置路由</a></li><ul class="second_class_ul"><li><a href="#_lab2_3_4">1.动态单个注册</a></li><li><a href="#_lab2_3_5">2.设置路由-动态批量注册</a></li></ul><li><a href="#_label4">五、变量规则</a></li><ul class="second_class_ul"></ul><li><a href="#_label5">六、路由参数</a></li><ul class="second_class_ul"></ul><li><a href="#_label6">七、资源路由</a></li><ul class="second_class_ul"></ul><li><a href="#_label7">八、快捷路由</a></li><ul class="second_class_ul"></ul><li><a href="#_label8">九、url生成</a></li><ul class="second_class_ul"></ul></ul></div><p>本文详细介绍了ThinkPHP5.0的路由功能,包括路由的作用、入口文件配置、路由模式(普通、混合、强制)、路由设置方法(动态单个注册、动态批量注册、配置文件批量注册)、变量规则、路由参数、资源路由的声明和自动注册规则,以及快捷路由的声明和控制器使用。此外,还讲解了如何生成URL以及隐藏入口文件的设置。</p>
<p class="maodian"><a name="_label0"></a></p><h2><strong>一、路由的作用</strong></h2>
<p>简化URL地址,方便记忆有利于搜索引擎的优化</p>
<p class="maodian"><a name="_label1"></a></p><h2><strong>二、入口文件</strong></h2>
<p class="maodian"><a name="_lab2_1_0"></a></p><h3>前后台分离</h3>
<p>在网站public目录下(项目\public)新建admin.php</p>
<p>打开admin.php</p>
<div class="jb51code"><pre class="brush:php;">&lt;?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 &lt;liu21st@gmail.com&gt;
// +----------------------------------------------------------------------

// [ 应用入口文件 ]

// 定义应用目录
define('APP_PATH', __DIR__ . '/../application/');
// 加载框架引导文件
require __DIR__ . '/../thinkphp/start.php';
</pre></div>
<p class="maodian"><a name="_lab2_1_1"></a></p><h3>绑定模块</h3>
<p>实现功能<br />index.php 这个入口文件,只能去前台模块<br />admin.php这个入口文件,只能去后台模块(建议后台入口文件复杂一些)</p>
<p>如何实现<br />在入口文件中</p>
<div class="jb51code"><pre class="brush:php;">// 定义前台
define('BIND_MODULE', 'index');
// 绑定后台
define('BIND_MODULE', 'admin');</pre></div>
<p>URL地址发生变化<br />入口绑定之前(http://www.tp.com/admin.php/模块/控制器/方法)<br />入口绑定之后(http://www.tp.com/admin.php/控制器/方法)</p>
<p class="maodian"><a name="_lab2_1_2"></a></p><h3>隐藏入口文件</h3>
<p>开启apache重写(D:\wamp64\bin\apache\apache2.4.23\conf\httpd.conf)<br />把注释开启 LoadModule rewrite_module modules/mod_rewrite.so</p>
<p>设置访问权限(D:\wamp64\bin\apache\apache2.4.23\conf\extra\httpd-vhosts.conf)</p>
<div class="jb51code"><pre class="brush:php;">&lt;VirtualHost *:80&gt;
    ServerName www.tp.com
    DocumentRoot D:/wamp64/www/study/thinkphpstudy/public
    &lt;Directory"D:/wamp64/www/study/thinkphpstudy/public"&gt;
      Options +Indexes +Includes +FollowSymLinks +MultiViews
      AllowOverride All
      Require all granted
    &lt;/Directory&gt;
&lt;/VirtualHost&gt;</pre></div>
<p>入口文件,在网站public目录下新建.htaccess文件,</p>
<div class="jb51code"><pre class="brush:php;">&lt;IfModule mod_rewrite.c&gt;
Options +FollowSymlinks -Multiviews
RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php/$1
&lt;/IfModule&gt;
</pre></div>
<p>重启服务</p>
<p>url地址变化<br />隐藏之前http://www.tp.com/index.php/控制器/方法<br />隐藏之后http://www.tp.com/控制器/方法</p>
<p class="maodian"><a name="_label2"></a></p><h2><strong>三、tp5.0路由学习注意</strong></h2>
<p>支持三种方式的url解析规则路由只针对应用,不针对模块,因此路由的设置也是针对应用下的所有模块。</p>
<p>关闭后台模块,在后台入口文件中(admin.php),写在加载框架引导文件之后,否则报错。</p>
<div class="jb51code"><pre class="brush:php;">&lt;?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 &lt;liu21st@gmail.com&gt;
// +----------------------------------------------------------------------

// [ 应用入口文件 ]

// 定义应用目录
define('APP_PATH', __DIR__ . '/../application/');
// 绑定后台
define('BIND_MODULE', 'admin');
// 加载框架引导文件
require __DIR__ . '/../thinkphp/start.php';

// 关闭admin模块的路由
\think\App::route(false);
</pre></div>
<p class="maodian"><a name="_lab2_2_3"></a></p><h3>路由的三种模式</h3>
<p><strong>普通模式</strong></p>
<p><strong>1.定义</strong><br />关闭路由,完全使用默认的PATH_INFO方式URL:<strong>2.形式</strong><br />http://www.tp.com/admin.php/index/index</p>
<p><strong>3.如何设置</strong></p>
<div class="jb51code"><pre class="brush:php;">   // 是否开启路由
    'url_route_on'         =&gt; false,
    // 是否强制使用路由
    'url_route_must'         =&gt; false,</pre></div>
<p><strong>混合模式</strong></p>
<p><strong>1.定义</strong><br />开启路由,并使用路由定义+默认PATH_INFO方式的混合</p>
<p><strong>2.如何设置</strong></p>
<div class="jb51code"><pre class="brush:php;">   // 是否开启路由
    'url_route_on'         =&gt; true,
    // 是否强制使用路由
    'url_route_must'         =&gt; false,
</pre></div>
<p><strong>强制模式</strong></p>
<p><strong>1.定义</strong><br />开启路由,并设置必需定义路由才能访问</p>
<p><strong>2.如何设置</strong></p>
<div class="jb51code"><pre class="brush:php;">   // 是否开启路由
    'url_route_on'         =&gt; true,
    // 是否强制使用路由
    'url_route_must'         =&gt; true,</pre></div>
<p class="maodian"><a name="_label3"></a></p><h2><strong>四、设置路由</strong></h2>
<p class="maodian"><a name="_lab2_3_4"></a></p><h3>1.动态单个注册</h3>
<p>设置路由格式</p>
<p>Route::rule(&lsquo;路由表达式&rsquo;, &lsquo;路由地址&rsquo;, &lsquo;请求类型&rsquo;, &lsquo;路由参数(数组)&rsquo;, &lsquo;变量规则(数组)&rsquo;)</p>
<p>设置路由文件(项目\application\route.php)</p>
<p>如何设置(route.php)</p>
<div class="jb51code"><pre class="brush:php;">use think\Route;
// 定义路由规则
// 设置路由之后,就不能使用pathinfo访问了
Route::rule('/','index/index/index');
//注册路由访问到index模块下的index控制器下的index的方法
Route::rule('test','index/index/test');
//注册路由test 访问到index模块下的index控制器下的test的方法</pre></div>
<p>路由的形式<br />1、静态地址路由</p>
<div class="jb51code"><pre class="brush:php;">//注册路由test 访问到index模块下的index控制器下的test的方法
Route::rule('test','index/index/test');</pre></div>
<p></p>
<p>2、给路由带参数</p>
<div class="jb51code"><pre class="brush:php;">route.php中
//注册带参数路由
//http://www.tp.com/course/1
//http://www.tp.com/index/index/index/id/1
Route::rule('course/:id','index/index/course');</pre></div>
<div class="jb51code"><pre class="brush:php;">index.php中
function course(){
            return input('id');
      }</pre></div>
<p>3、给路由带多个参数(设置了带几个就必需带几个)</p>
<div class="jb51code"><pre class="brush:php;">route.php中
//注册带参数路由
//http://www.tp.com/time/1/2
//http://www.tp.com/index/index/shijian/year/1/month/2
Route::rule('time/:year/:month','index/index/shijian');</pre></div>
<div class="jb51code"><pre class="brush:php;">index.php中
function shijian(){
      return input('year').input('month');
    }</pre></div>
<p>4、可选参数路由</p>
<div class="jb51code"><pre class="brush:php;">route.php中
//注册带可选参数路由
//http://www.tp.com/time/1
//http://www.tp.com/index/index/shijian/year/1
Route::rule('time/:year/[:month]','index/index/shijian');</pre></div>
<p></p>
<div class="jb51code"><pre class="brush:php;">index.php中
function shijian(){
      return input('year').input('month');
    }</pre></div>
<p>5、全动态路由(不建议使用)</p>
<div class="jb51code"><pre class="brush:php;">    route.php中
    //注册带可选参数路由
    //http://www.tp.com/1/1
    //http://www.tp.com/index/index/dongtai/1/1
    Route::rule(':a/:b','index/index/dongtai');</pre></div>
<div class="jb51code"><pre class="brush:php;">    index.php中
    function dongtai(){
      return input('a').input('b');
    }</pre></div>
<p>6、完全匹配路由</p>
<div class="jb51code"><pre class="brush:php;">    route.php中
    //注册带可选参数路由
    //http://www.tp.com/1/1
    //http://www.tp.com/index/index/dongtai/1/1
    Route::rule(':a/:b$','index/index/dongtai');</pre></div>
<p></p>
<div class="jb51code"><pre class="brush:php;">    index.php中
    function dongtai(){
      return input('a').input('b');
    }</pre></div>
<p>7、带额外参数</p>
<div class="jb51code"><pre class="brush:php;">    route.php中
    // 带额外参数
    Route::rule('test2','index/index/test2?id=10&amp;name=tian');</pre></div>
<div class="jb51code"><pre class="brush:php;">    index.php中
    function test2(){
      dump(input());
    }</pre></div>
<p><strong>设置请求类型</strong><br /><strong>1.TP中请求类型</strong><br />get,post,put,delete<br /><strong>2.Route::rule() 默认支持所有类型</strong><br /><strong>3.设置各种请求</strong></p>
<div class="jb51code"><pre class="brush:php;">// 支持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和post
Route::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' );</pre></div>
<p><strong>4.如何模拟put和delete请求</strong></p>
<div class="jb51code"><pre class="brush:php;">&lt;form action="type" method="post"&gt;
    &lt;p&gt;
      &lt;input type="hidden" name="_method" value="PUT" /&gt;
      &lt;input type="text" name="name" id="" /&gt;
    &lt;/p &gt;
    &lt;p&gt;
      &lt;input type="submit" value="提交" /&gt;
    &lt;/p &gt;
&lt;/form&gt;</pre></div>
<p></p>
<p class="maodian"><a name="_lab2_3_5"></a></p><h3>2.设置路由-动态批量注册</h3>
<p><strong>1.基本格式</strong></p>
<div class="jb51code"><pre class="brush:php;">    Route::rule([
      '路由规则1'=&gt;'路由地址和参数',
      '路由规则2'=&gt;['路由地址和参数','匹配参数(数组)','变量规则(数组)'],
      ...
    ],'','请求类型','匹配参数(数组)','变量规则');</pre></div>
<p></p>
<p><strong>2.使用</strong></p>
<div class="jb51code"><pre class="brush:php;">// 动态批量注册
Route::rule([
      "index"=&gt;"index/index/index",
      "diaoyong"=&gt;"index/index/diaoyong",
      "type/:id"=&gt;"index/index/type"
    ],'','get');
Route::get([
      "index"=&gt;"index/index/index",
      "diaoyong"=&gt;"index/index/diaoyong",
      "type/:id"=&gt;"index/index/type"
    ]);</pre></div>
<p></p>
<p><strong>3.设置路由-配置文件批量注册</strong></p>
<div class="jb51code"><pre class="brush:php;">return [
    "index"=&gt;"index/index/index",
    "diaoyong"=&gt;"index/index/diaoyong",
    "type/:id"=&gt;"index/index/type"
];
</pre></div>
<p class="maodian"><a name="_label4"></a></p><h2><strong>五、变量规则</strong></h2>
<blockquote><p>Route::rule(&lsquo;路由表达式&rsquo;,&rsquo;路由地址&rsquo;,&rsquo;请求类型&rsquo;,&rsquo;路由参数(数组)&rsquo;,&rsquo;变量规则(数组)&rsquo;);</p></blockquote>
<div class="jb51code"><pre class="brush:php;">// ['id'=&gt;'\d{1,3}','name'=&gt;'\w+']设置路由变量规则,id只能是1-3位数字,name只能是hi字符串
Route::rule("course/:id/:name","index/index/course",'get',[],['id'=&gt;'\d{1,3}','name'=&gt;'\w+']);</pre></div>
<p class="maodian"><a name="_label5"></a></p><h2><strong>六、路由参数</strong></h2>
<p>路由参数是指可以设置一些路由匹配的条件参数,主要用于验证当前的路由规则是否有效,主要包括:</p>
<div class="jb51code"><pre class="brush:php;">Route::rule('course/:id/:name','index/index/course','get',['method'=&gt;'get','ext'=&gt;'html'],['id'=&gt;'\d{1,3}','name'=&gt;'\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.1+)
cache   请求缓存(V5.0.1+)
param_depr路由参数分隔符(V5.0.2+)
ajax    Ajax检测(V5.0.2+)
pjax    Pjax检测(V5.0.2+)</pre></div>
<p class="maodian"><a name="_label6"></a></p><h2><strong>七、资源路由</strong></h2>
<p><strong>1.声明</strong></p>
<blockquote><p>Route::resource(&lsquo;blog&rsquo;,&rsquo;index/blog&rsquo;);</p></blockquote>
<p>也可以在定义资源路由的时候限定执行的方法(标识),例如:</p>
<div class="jb51code"><pre class="brush:php;">Route::resource('blog','index/blog',['only'=&gt;['index','read','edit','update']]);
Route::resource('blog','index/blog',['except'=&gt;['index','delete']]);</pre></div>
<p><strong>2.会动注册7个路由规则(一定要记忆)</strong></p>
<table><thead><tr><th>标识</th><th>请求类型</th><th>生成路由规则</th><th>对应操作方法(默认)</th></tr></thead><tbody><tr><td>index</td><td>GET</td><td>blog</td><td>index</td></tr><tr><td>create</td><td>GET</td><td>blog/create</td><td>create</td></tr><tr><td>save</td><td>POST</td><td>blog</td><td>save</td></tr><tr><td>read</td><td>GET</td><td>blog/:id</td><td>read</td></tr><tr><td>edit</td><td>GET</td><td>blog/:id/edit</td><td>edit</td></tr><tr><td>update</td><td>PUT</td><td>blog/:id</td><td>update</td></tr><tr><td>delete</td><td>DELETE</td><td>blog/:id</td><td>delete</td></tr></tbody></table>
<p class="maodian"><a name="_label7"></a></p><h2><strong>八、快捷路由</strong></h2>
<p><strong>1.声明</strong></p>
<div class="jb51code"><pre class="brush:php;">// 声明快捷路由
Route::controller('blog','index/blog');
</pre></div>
<p><strong>2.控制器</strong></p>
<div class="jb51code"><pre class="brush:php;">class Blog
{
    public function geta(){
      echo 'aaaaaaaaa';
    }
}
</pre></div>
<p></p>
<p><strong>3.url访问</strong></p>
<p>https://www.tp.com/blog/a (寻找geta方法)<br />https://www.tp.com/blog/index (寻找getindex方法)</p>
<p class="maodian"><a name="_label8"></a></p><h2><strong>九、url生成</strong></h2>
<p><strong>1.系统类</strong></p>
<div class="jb51code"><pre class="brush:php;">dump(Url::build('index/index/index'));
</pre></div>
<p><strong>2.系统方法</strong></p>
<div class="jb51code"><pre class="brush:php;">dump(url('index/index/index'));
</pre></div>
<p><strong>3.使用</strong></p>
<div class="jb51code"><pre class="brush:php;">public function index()
    {
      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',['id'=&gt;10,'name'=&gt;'张三']));
      dump(url('index/index/abc',['id'=&gt;10,'name'=&gt;'张三']));
      dump(url('index/index/abc','id=10&amp;name=100'));

      //带锚点
      dump(url('index/blog/read#name','id=5'));
      dump(url('index/blog/read#name',['id'=&gt;5,'name'=&gt;'100']));
      // 带域名
      dump(Url::build('index/blog/read#anchor@blog','id=5'));
      dump(url('index/blog/read#anchor@blog',['id'=&gt;5,'name'=&gt;'100']));
      http://blog.tp.com/blog/read/id/5/name/100.html#anchor

      // 加上入口文件
      Url::root('/index.php');
      dump(url('index/blog/read#anchor@blog',['id'=&gt;5,'name'=&gt;'100']));
      //http://blog.tp.com/index.php/blog/read/id/5/name/100.html#anchor

    }
</pre></div>
頁: [1]
查看完整版本: thinkphp底层原理速成:入口文件、路由模式、路由设置和url生成