yxrj 发表于 2020-4-17 19:42:21

ThinkPHP5—路由(route)详解

<h1 class="postTitle" style="margin-bottom: 20px; font-size: 28px; font-weight: 400; line-height: 1.8; color: rgb(51, 51, 51); font-family: Verdana, Arial, Helvetica, sans-serif;"><a id="cb_post_title_url" class="postTitle2" href="https://www.cnblogs.com/bushui/p/11771792.html" style="color: rgb(51, 51, 51); text-decoration-line: none;">ThinkPHP5——route(路由)的详解</a></h1><div class="clear" style="clear: both; color: rgb(125, 139, 141); font-family: Verdana, Arial, Helvetica, sans-serif;"></div><div class="postBody" style="color: rgb(125, 139, 141); font-family: Verdana, Arial, Helvetica, sans-serif;"><div id="cnblogs_post_body" class="blogpost-body " style="margin-bottom: 20px; word-break: break-word; color: rgb(51, 51, 51); line-height: 1.8;"><p style="margin: 10px auto;"><span style="font-size: 16px;">  路由在框架中的作用打个比方的话,路由好比是WEB应用的总调度室,对于访问的URL地址,路由可以拒绝或者接受某个URL请求,并进行分发调度,而且还有一个副作用是因为路由规则可以随意定义,因此可以让你的URL请求地址更优雅,因为不会暴露实际的URL地址,也就意味着更安全,5.0的路由不仅仅只是支持路由到控制器的操作方法,甚至可以路由到任何的类或者闭包。</span></p><p style="margin: 10px auto;">&nbsp;</p><p style="margin: 10px auto;"><span style="font-size: 18pt;"><strong>1、路由模式</strong></span></p><p style="margin: 10px auto;"><span style="font-size: 16px;">路由可以理解为一种寻径功能模块,比如URL地址里面的index模块怎么才能省略呢,默认的URL地址显得有点长,下面就来说说如何通过路由简化URL访问。ThinkPHP5.0的路由比较灵活,并且不需要强制定义,可以总结归纳为如下三种方式:</span></p><p style="margin: 10px auto;"><span style="font-size: 18px;"><strong>1.1</strong>、<strong>普通模式</strong></span></p><p style="margin: 10px auto;"><span style="font-size: 16px;">关闭路由,完全使用默认的PATH_INFO方式URL:</span></p><div class="cnblogs_code" style="margin-top: 5px; margin-bottom: 5px; padding: 5px; background-color: rgb(245, 245, 245); border: 1px solid rgb(204, 204, 204); overflow: auto; color: rgb(0, 0, 0); font-family: &quot;Courier New&quot; !important; font-size: 12px !important;"><pre style="white-space: pre-wrap; font-family: &quot;Courier New&quot; !important;">'url_route_on'=&gt;<span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">false</span>,</pre></div><p style="margin: 10px auto;"><span style="font-size: 16px;">路由关闭后,不会解析任何路由规则,采用默认的PATH_INFO 模式访问URL:</span></p><div class="cnblogs_code" style="margin-top: 5px; margin-bottom: 5px; padding: 5px; background-color: rgb(245, 245, 245); border: 1px solid rgb(204, 204, 204); overflow: auto; color: rgb(0, 0, 0); font-family: &quot;Courier New&quot; !important; font-size: 12px !important;"><pre style="white-space: pre-wrap; font-family: &quot;Courier New&quot; !important;">http:<span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">//</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">serverName/index.php/module/controller/action/param/value/...</span></pre></div><p style="margin: 10px auto;"><span style="font-size: 16px;">但仍然可以通过操作方法的参数绑定、空控制器和空操作等特性实现URL地址的简化。</span></p><p style="margin: 10px auto;"><span style="font-size: 16px;">可以设置<code>url_param_type</code>配置参数来改变pathinfo模式下面的参数获取方式,默认是按名称成对解析,支持按照顺序解析变量,只需要更改为:</span></p><div class="cnblogs_code" style="margin-top: 5px; margin-bottom: 5px; padding: 5px; background-color: rgb(245, 245, 245); border: 1px solid rgb(204, 204, 204); overflow: auto; color: rgb(0, 0, 0); font-family: &quot;Courier New&quot; !important; font-size: 12px !important;"><pre style="white-space: pre-wrap; font-family: &quot;Courier New&quot; !important;"><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">//</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;"> 按照顺序解析变量</span>
'url_param_type'    =&gt;1,</pre></div><p style="margin: 10px auto;"><span style="font-size: 18px;"><strong>1.2、混合模式</strong></span></p><p style="margin: 10px auto;"><span style="font-size: 16px;">开启路由,并使用路由定义+默认PATH_INFO方式的混合:</span></p><div class="cnblogs_code" style="margin-top: 5px; margin-bottom: 5px; padding: 5px; background-color: rgb(245, 245, 245); border: 1px solid rgb(204, 204, 204); overflow: auto; color: rgb(0, 0, 0); font-family: &quot;Courier New&quot; !important; font-size: 12px !important;"><pre style="white-space: pre-wrap; font-family: &quot;Courier New&quot; !important;">'url_route_on' =&gt; <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">true</span>,
'url_route_must'=&gt; <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">false</span>,</pre></div><p style="margin: 10px auto;"><span style="font-size: 16px;">该方式下面,只需要对需要定义路由规则的访问地址定义路由规则,其它的仍然按照第一种普通模式的PATH_INFO模式访问URL。</span></p><p style="margin: 10px auto;"><span style="font-size: 18px;"><strong>1.3、强制模式</strong></span></p><p style="margin: 10px auto;"><span style="font-size: 16px;">开启路由,并设置必须定义路由才能访问:</span></p><div class="cnblogs_code" style="margin-top: 5px; margin-bottom: 5px; padding: 5px; background-color: rgb(245, 245, 245); border: 1px solid rgb(204, 204, 204); overflow: auto; color: rgb(0, 0, 0); font-family: &quot;Courier New&quot; !important; font-size: 12px !important;"><pre style="white-space: pre-wrap; font-family: &quot;Courier New&quot; !important;">'url_route_on'          =&gt;<span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">true</span>,
'url_route_must'      =&gt;<span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">true</span>,</pre></div><p style="margin: 10px auto;">这种方式下面必须严格给每一个访问地址定义路由规则(包括首页),否则将抛出异常。</p><p style="margin: 10px auto;">首页的路由规则采用<code>/</code>定义即可,例如下面把网站首页路由输出<code>Hello,world!</code></p><div class="cnblogs_code" style="margin-top: 5px; margin-bottom: 5px; padding: 5px; background-color: rgb(245, 245, 245); border: 1px solid rgb(204, 204, 204); overflow: auto; color: rgb(0, 0, 0); font-family: &quot;Courier New&quot; !important; font-size: 12px !important;"><pre style="white-space: pre-wrap; font-family: &quot;Courier New&quot; !important;">Route::get('/',<span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">function</span><span style="line-height: 1.5 !important;">(){
    </span><span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">return</span> 'Hello,world!'<span style="line-height: 1.5 !important;">;
});</span></pre></div><p style="margin: 10px auto;">&nbsp;</p><h3 style="margin-top: 10px; margin-bottom: 10px; font-size: 16px; line-height: 1.5;"><span style="font-size: 18pt;">2、路由配置</span></h3><p style="margin: 10px auto;"><span style="font-size: 16px;"><strong>2.1<strong><code class="language-rust">、</code></strong>URL请求的执行流程:</strong></span><span style="font-size: 16px;"><code class="language-rust"><strong>用户请求&nbsp;<span class="token punctuation">-&gt; 路由解析&nbsp;<span class="token punctuation">-&gt; 调度请求&nbsp;<span class="token punctuation">-&gt; 执行操作&nbsp;<span class="token punctuation">-&gt; 响应输出</span></span></span></span></strong></code></span></p><p style="margin: 10px auto;"><strong><span style="font-size: 18px;"><code class="language-rust"><span style="font-size: 16px;">2.2、路由规则写在哪里</span>:</code></span></strong></p><p class="line-numberslanguage-rust" style="margin: 10px auto;"><code class="language-rust"><span class="token operator">  * 路由规则写在与应用配置统计的route<span class="token punctuation">.php文件中</span></span></code></p><p class="line-numberslanguage-rust" style="margin: 10px auto;"><code class="language-rust"><span class="token operator"><span class="token punctuation">  <span class="token operator">* 路由规则主要使用路由类Route<span class="token punctuation">::<span class="token function">rule<span class="token punctuation">(<span class="token punctuation">)方法注册</span></span></span></span></span></span></span></code></p><p class="line-numberslanguage-rust" style="margin: 10px auto;"><strong><span class="token operator" style="font-size: 16px;"><span class="token punctuation"><span class="token operator"><span class="token punctuation"><span class="token function"><span class="token punctuation"><span class="token punctuation">2.3<strong><code class="language-rust">、</code></strong></span></span></span></span></span></span></span><span style="font-size: 16px;"><code class="ಠhighlight-container"><span class="token comment">设置路由配置文件列表:</span></code></span></strong></p><div class="cnblogs_code" style="margin-top: 5px; margin-bottom: 5px; padding: 5px; background-color: rgb(245, 245, 245); border: 1px solid rgb(204, 204, 204); overflow: auto; color: rgb(0, 0, 0); font-family: &quot;Courier New&quot; !important; font-size: 12px !important;"><pre style="white-space: pre-wrap; font-family: &quot;Courier New&quot; !important;">'route_config_file'    =&gt;    ['home','admin'],</pre></div><p style="margin: 10px auto;"><code>application/home.php</code>配置<code>home</code>模块的路由规则,<code>application/admin.php</code>则配置<code>admin</code>模块的路由规则。</p><p style="margin: 10px auto;">虽然运行的时候依然会同时加载并注册,但定义的时候是明确分开了,便于协作。</p><p class="line-numberslanguage-rust" style="margin: 10px auto;">&nbsp;</p><p style="margin: 10px auto;"><span style="font-size: 18pt;"><strong>3、路由注册</strong></span></p><p style="margin: 10px auto;"><span style="font-size: 16px;">路由注册可以采用方法动态单个和批量注册,也可以直接定义路由定义文件的方式进行集中注册。</span></p><p style="margin: 10px auto;"><span style="font-size: 18px;"><strong>3.1、动态注册和静态路由</strong></span></p><p style="margin: 10px auto;"><span style="font-size: 16px;">现在给该URL地址定义一个新的路由规则如下:</span></p><div class="cnblogs_code" style="margin-top: 5px; margin-bottom: 5px; padding: 5px; background-color: rgb(245, 245, 245); border: 1px solid rgb(204, 204, 204); overflow: auto; color: rgb(0, 0, 0); font-family: &quot;Courier New&quot; !important; font-size: 12px !important;"><pre style="white-space: pre-wrap; font-family: &quot;Courier New&quot; !important;">Route::rule('hello/:name','index/Index/hello');</pre></div><p style="margin: 10px auto;"><span style="font-size: 16px;">现在我们来分析下<code>rule</code>方法的参数,第一个参数称为<strong>路由规则</strong>(通过路由访问的地址),第二个参数为该规则对应的<strong>路由地址</strong>(也就是原来定义路由之前访问的URL地址)。</span></p><p style="margin: 10px auto;"><span style="font-size: 16px;">路由规则通常可以包含变量(例如其中的<code>:name</code>就是一个路由变量),路由规则中包含变量(包括可选变量)的就称该条路由规则为<strong>动态路由</strong>,没有包含任何变量的路由我们称之为<strong>静态路由</strong>,例如:<br></span></p><div class="cnblogs_code" style="margin-top: 5px; margin-bottom: 5px; padding: 5px; background-color: rgb(245, 245, 245); border: 1px solid rgb(204, 204, 204); overflow: auto; color: rgb(0, 0, 0); font-family: &quot;Courier New&quot; !important; font-size: 12px !important;"><pre style="white-space: pre-wrap; font-family: &quot;Courier New&quot; !important;"><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">//</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;"> 静态路由规则</span>
Route::rule('hello','index/Index/hello'<span style="line-height: 1.5 !important;">);
</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">//</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;"> 动态路由规则</span>
Route::rule('hello/:name','index/Index/hello');</pre></div><p style="margin: 10px auto;">注:ThinkPHP5.0的路由规则定义是从根目录开始,而不是基于模块名的。<strong>并且原来的访问地址会自动失效。</strong></p><p style="margin: 10px auto;"><span style="font-size: 18px;"><strong>3.2、URL请求类型</strong></span></p><p style="margin: 10px auto;"><span style="font-size: 16px;">我们知道一个URL请求类型有很多,常用的包括<code>GET</code>/&nbsp;<code>POST</code>/&nbsp;<code>PUT</code>/&nbsp;<code>DELETE</code>等,<strong>我们使用<code>rule</code>方法注册的路由,默认是支持任意请求类型访问的</strong>,不过你可以通过第三个参数来限定请求类型:</span></p><div class="cnblogs_code" style="margin-top: 5px; margin-bottom: 5px; padding: 5px; background-color: rgb(245, 245, 245); border: 1px solid rgb(204, 204, 204); overflow: auto; color: rgb(0, 0, 0); font-family: &quot;Courier New&quot; !important; font-size: 12px !important;"><pre style="white-space: pre-wrap; font-family: &quot;Courier New&quot; !important;">Route::rule('hello/:name','index/index/hello','GET');</pre></div><p style="margin: 10px auto;"><span style="font-size: 16px;"><strong>注:</strong>只有通过<code>GET</code>请求的访问,该路由才会生效。<strong>不指定的话默认为任何请求类型</strong></span></p><p style="margin: 10px auto;"><span style="font-size: 16px;">如果你希望路由可以支持<strong>所有的请求类型</strong>,也可以使用:</span></p><div class="cnblogs_code" style="margin-top: 5px; margin-bottom: 5px; padding: 5px; background-color: rgb(245, 245, 245); border: 1px solid rgb(204, 204, 204); overflow: auto; color: rgb(0, 0, 0); font-family: &quot;Courier New&quot; !important; font-size: 12px !important;"><pre style="white-space: pre-wrap; font-family: &quot;Courier New&quot; !important;">Route::any('hello/:name','index/index/hello');</pre></div><p style="margin: 10px auto;"><span style="font-size: 16px;"><strong><code>注:any</code>方法其实和<code>rule</code>方法是一样的,区别在于不用写第三个参数。</strong></span></p><p style="margin: 10px auto;"><span style="font-size: 16px;">系统提供了为不同的请求类型定义路由规则的简化方法,例如:</span></p><div class="cnblogs_code" style="margin-top: 5px; margin-bottom: 5px; padding: 5px; background-color: rgb(245, 245, 245); border: 1px solid rgb(204, 204, 204); overflow: auto; color: rgb(0, 0, 0); font-family: &quot;Courier New&quot; !important; font-size: 12px !important;"><pre style="white-space: pre-wrap; font-family: &quot;Courier New&quot; !important;">Route::get('new/:id','News/read'); <span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">//</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;"> 定义GET请求路由规则</span>
Route::post('new/:id','News/update'); <span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">//</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;"> 定义POST请求路由规则</span>
Route::put('new/:id','News/update'); <span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">//</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;"> 定义PUT请求路由规则</span>
Route::delete('new/:id','News/delete'); <span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">//</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;"> 定义DELETE请求路由规则</span>
Route::any('new/:id','News/read'); <span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">//</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;"> 所有请求都支持的路由规则</span></pre></div><p style="margin: 10px auto;"><span style="font-size: 16px;">我们也可以批量注册路由规则,例如:</span></p><div class="cnblogs_code" style="margin-top: 5px; margin-bottom: 5px; padding: 5px; background-color: rgb(245, 245, 245); border: 1px solid rgb(204, 204, 204); overflow: auto; color: rgb(0, 0, 0); font-family: &quot;Courier New&quot; !important; font-size: 12px !important;"><pre style="white-space: pre-wrap; font-family: &quot;Courier New&quot; !important;">Route::rule(['new/:id'=&gt;'News/read','blog/:name'=&gt;'Blog/detail'<span style="line-height: 1.5 !important;">]);
Route</span>::get(['new/:id'=&gt;'News/read','blog/:name'=&gt;'Blog/detail'<span style="line-height: 1.5 !important;">]);
Route</span>::post(['new/:id'=&gt;'News/update','blog/:name'=&gt;'Blog/detail']);</pre></div><p style="margin: 10px auto;">&nbsp;</p><p style="margin: 10px auto;"><strong><span style="font-size: 18pt;">4、路由表达式</span></strong></p><p style="margin: 10px auto;"><span style="font-size: 16px;">路由表达式统一使字符串定义,采用规则定义的方式。</span></p><p style="margin: 10px auto;"><strong><span style="font-size: 18px;">4.1、规则表达式</span></strong></p><p style="margin: 10px auto;"><span style="font-size: 16px;">规则表达式通常包含静态地址和动态地址,或者两种地址的结合,例如下面都属于有效的规则表达式:</span></p><div class="cnblogs_code" style="margin-top: 5px; margin-bottom: 5px; padding: 5px; background-color: rgb(245, 245, 245); border: 1px solid rgb(204, 204, 204); overflow: auto; color: rgb(0, 0, 0); font-family: &quot;Courier New&quot; !important; font-size: 12px !important;"><pre style="white-space: pre-wrap; font-family: &quot;Courier New&quot; !important;">'/' =&gt; 'index', <span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">//</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;"> 首页访问路由</span>
'my'      =&gt;'Member/myinfo', <span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">//</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;"> 静态地址路由</span>
'blog/:id'=&gt;'Blog/read', <span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">//</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;"> 静态地址和动态地址结合</span>
'new/:year/:month/:day'=&gt;'News/read', <span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">//</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;"> 静态地址和动态地址结合</span>
':user/:blog_id'=&gt;'Blog/read',<span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">//</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;"> 全动态地址</span></pre></div><p style="margin: 10px auto;">每个参数中以“:”开头的参数都表示动态变量,并且会自动绑定到操作方法的对应参数。</p><p style="margin: 10px auto;"><span style="font-size: 18px;"><strong>4.2、路由变量</strong></span></p><p style="margin: 10px auto;"><span style="font-size: 16px;">支持对路由参数的可选定义,例如:</span></p><div class="cnblogs_code" style="margin-top: 5px; margin-bottom: 5px; padding: 5px; background-color: rgb(245, 245, 245); border: 1px solid rgb(204, 204, 204); overflow: auto; color: rgb(0, 0, 0); font-family: &quot;Courier New&quot; !important; font-size: 12px !important;"><pre style="white-space: pre-wrap; font-family: &quot;Courier New&quot; !important;">'blog/:year/[:month]'=&gt;'Blog/archive',</pre></div><p style="margin: 10px auto;"><code>[:month]</code>变量用<code>[ ]</code>包含起来后就表示该变量是路由匹配的可选变量。</p><p style="margin: 10px auto;"><span style="font-size: 18px;"><strong>4.3、变量解析方式</strong></span></p><p style="margin: 10px auto;"><span style="font-size: 16px;">路由规则之外的变量解析方式一般是<code>key1/value1/key2/value2</code>解析为<code>key1=value1</code>,<code>key2=value2</code>,也就是说</span></p><div class="cnblogs_code" style="margin-top: 5px; margin-bottom: 5px; padding: 5px; background-color: rgb(245, 245, 245); border: 1px solid rgb(204, 204, 204); overflow: auto; color: rgb(0, 0, 0); font-family: &quot;Courier New&quot; !important; font-size: 12px !important;"><pre style="white-space: pre-wrap; font-family: &quot;Courier New&quot; !important;">http:<span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">//</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">tp5.com/hello/thinkphp/city/shanghai</span></pre></div><p style="margin: 10px auto;"><span style="font-size: 16px;">除了会解析路由变量<code>name</code>之外,还会解析另外一个<code>city</code>变量,分别是:</span></p><div class="cnblogs_code" style="margin-top: 5px; margin-bottom: 5px; padding: 5px; background-color: rgb(245, 245, 245); border: 1px solid rgb(204, 204, 204); overflow: auto; color: rgb(0, 0, 0); font-family: &quot;Courier New&quot; !important; font-size: 12px !important;"><pre style="white-space: pre-wrap; font-family: &quot;Courier New&quot; !important;">'name'    =&gt;    'thinkphp',
'city'    =&gt;    'shanghai'</pre></div><p style="margin: 10px auto;"><span style="font-size: 16px;">当然我们可以设置按顺序解析:</span></p><div class="cnblogs_code" style="margin-top: 5px; margin-bottom: 5px; padding: 5px; background-color: rgb(245, 245, 245); border: 1px solid rgb(204, 204, 204); overflow: auto; color: rgb(0, 0, 0); font-family: &quot;Courier New&quot; !important; font-size: 12px !important;"><pre style="white-space: pre-wrap; font-family: &quot;Courier New&quot; !important;">'url_param_type'    =&gt;    1</pre></div><p style="margin: 10px auto;"><span style="font-size: 16px;">这个时候如果访问<code class="ಠhighlight-container">http<span class="token punctuation">:<span class="token operator">/<span class="token operator">/tp5<span class="token punctuation">.com<span class="token operator">/hello<span class="token operator">/thinkphp<span class="token operator">/city<span class="token operator">/shanghai,得到的变量结果就完全不同了:</span></span></span></span></span></span></span></span></code></span></p><div class="cnblogs_code" style="margin-top: 5px; margin-bottom: 5px; padding: 5px; background-color: rgb(245, 245, 245); border: 1px solid rgb(204, 204, 204); overflow: auto; color: rgb(0, 0, 0); font-family: &quot;Courier New&quot; !important; font-size: 12px !important;"><pre style="white-space: pre-wrap; font-family: &quot;Courier New&quot; !important;">'name'    =&gt;    'thinkphp',
0    =&gt;    'city',
1    =&gt;    'shanghai',</pre></div><p style="margin: 10px auto;"><span style="font-size: 18px;"><strong>4.4、</strong></span><strong><span style="font-size: 18px;">定制分隔符</span></strong></p><p style="margin: 10px auto;"><span style="font-size: 16px;">我们在定义路由规则的时候,都是统一使用<code>/</code>作为URL分隔符,但并不是表示URL访问的时候只能使用<code>/</code>作为分隔符,例如我们可以设置参数:</span></p><div class="cnblogs_code" style="margin-top: 5px; margin-bottom: 5px; padding: 5px; background-color: rgb(245, 245, 245); border: 1px solid rgb(204, 204, 204); overflow: auto; color: rgb(0, 0, 0); font-family: &quot;Courier New&quot; !important; font-size: 12px !important;"><pre style="white-space: pre-wrap; font-family: &quot;Courier New&quot; !important;">'pathinfo_depr'    =&gt;    '-',</pre></div><p style="margin: 10px auto;"><span style="font-size: 16px;">那么URL访问地址就会变成</span></p><div class="cnblogs_code" style="margin-top: 5px; margin-bottom: 5px; padding: 5px; background-color: rgb(245, 245, 245); border: 1px solid rgb(204, 204, 204); overflow: auto; color: rgb(0, 0, 0); font-family: &quot;Courier New&quot; !important; font-size: 12px !important;"><pre style="white-space: pre-wrap; font-family: &quot;Courier New&quot; !important;">http:<span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">//</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">tp5.com/hello-thinkphp</span>
http:<span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">//</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">tp5.com/hello-thinkphp-beijing</span></pre></div><p style="margin: 10px auto;"><span style="font-size: 16px;">如果你希望某个路由地址使用<strong>不同的URL分隔符</strong>,有两种方法:</span></p><p style="margin: 10px auto;"><span style="font-size: 16px;"><strong><code>方法一:param_depr</code></strong><br></span></p><p style="margin: 10px auto;"><span style="font-size: 16px;">先介绍第一种比较简单的,定义路由规则的时候,添加<strong><code>param_depr</code></strong>参数(要求<code>V5.0.2+</code>),例如:</span></p><div class="cnblogs_code" style="margin-top: 5px; margin-bottom: 5px; padding: 5px; background-color: rgb(245, 245, 245); border: 1px solid rgb(204, 204, 204); overflow: auto; color: rgb(0, 0, 0); font-family: &quot;Courier New&quot; !important; font-size: 12px !important;"><pre style="white-space: pre-wrap; font-family: &quot;Courier New&quot; !important;">Route::get('hello/:name/[:city]','index/index/hello',['param_depr' =&gt; '-'],[ 'name' =&gt; '\w+' , 'city' =&gt; '+' ]);</pre></div><p style="margin: 10px auto;"><span style="font-size: 16px;">表示只有在该路由规则访问的时候,才使用<code>-</code>作为url分隔符。</span></p><p style="margin: 10px auto;"><strong><span style="font-size: 16px;">方法二:组合变量</span></strong></p><p style="margin: 10px auto;"><span style="font-size: 16px;">在一些复杂的路由规则定义中,我们可以使用组合变量定义方式,<strong>组合变量的优势是变量分隔符可以随意定义</strong>,例如:</span></p><div><div class="cnblogs_code" style="margin-top: 5px; margin-bottom: 5px; padding: 5px; background-color: rgb(245, 245, 245); border: 1px solid rgb(204, 204, 204); overflow: auto; color: rgb(0, 0, 0); font-family: &quot;Courier New&quot; !important; font-size: 12px !important;"><div class="cnblogs_code_toolbar" style="margin-top: 5px;"><span class="cnblogs_code_copy" style="padding-right: 5px; line-height: 1.5 !important;"><a title="复制代码" style="color: rgb(0, 0, 0); text-decoration-line: underline; border: none !important;"><img src="https://common.cnblogs.com/images/copycode.gif" alt="复制代码" style="height: auto; max-width: 800px;"></a></span></div><pre style="white-space: pre-wrap; font-family: &quot;Courier New&quot; !important;"><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">//</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">正确路径:http://www.mtp5.com/test/1;注:‘?《name?》’表示可选的,['index/index2/test', []]里面的‘[]’是必须的</span>
    'test/&lt;id?&gt;-?&lt;name?&gt;'=&gt;['index/index2/test',<span style="line-height: 1.5 !important;"> []]

    </span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">//</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">正确路径:http://www.mtp5.com/test-1</span>
    'test-&lt;id&gt;-?&lt;name?&gt;'=&gt;['index/index2/test',<span style="line-height: 1.5 !important;"> []]

    </span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">//</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">正确路径:http://www.mtp5.com/test-1-</span>
    'test-&lt;id&gt;-&lt;name?&gt;'=&gt;['index/index2/test',<span style="line-height: 1.5 !important;"> []]

    </span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">//</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">正确路径:http://www.mtp5.com/test/1-</span>
    'test/&lt;id&gt;-&lt;name?&gt;'=&gt;['index/index2/test',<span style="line-height: 1.5 !important;"> []]

    </span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">//</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">正确路径:http://www.mtp5.com/test1</span>
    'test?&lt;id?&gt;-?&lt;name?&gt;'=&gt;['index/index2/test',<span style="line-height: 1.5 !important;"> []]

    </span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">//</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">正确路径:http://www.mtp5.com/test</span>
    'test-?&lt;id?&gt;-?&lt;name?&gt;'=&gt;['index/index2/test', []]</pre><div class="cnblogs_code_toolbar" style="margin-top: 5px;"><span class="cnblogs_code_copy" style="padding-right: 5px; line-height: 1.5 !important;"><a title="复制代码" style="color: rgb(0, 0, 0); text-decoration-line: underline; border: none !important;"><img src="https://common.cnblogs.com/images/copycode.gif" alt="复制代码" style="height: auto; max-width: 800px;"></a></span></div></div><p style="margin: 10px auto;">错误的写法:</p><div class="cnblogs_code" style="margin-top: 5px; margin-bottom: 5px; padding: 5px; background-color: rgb(245, 245, 245); border: 1px solid rgb(204, 204, 204); overflow: auto; color: rgb(0, 0, 0); font-family: &quot;Courier New&quot; !important; font-size: 12px !important;"><pre style="white-space: pre-wrap; font-family: &quot;Courier New&quot; !important;"><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">//</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">错误写法</span>
    'test/?&lt;id?&gt;-?&lt;name?&gt;'=&gt;['index/index2/test',<span style="line-height: 1.5 !important;"> []]
    </span>'test/[:id]-?&lt;name?&gt;'=&gt;['index/index2/test', []]</pre></div></div><p style="margin: 10px auto;"><span style="font-size: 16px;">注:&lt;name?&gt;表示可选变量,'?&lt;name?&gt;'表示前面的分隔符和这个变量都是可选的</span></p><p style="margin: 10px auto;"><span style="font-size: 16px;">通过本篇内容的学习,你已经掌握了路由变量的使用,下面是路由变量的对比</span></p><table style="border-spacing: 0px; max-width: 850px; border: 1px solid rgb(192, 192, 192); word-break: break-word; width: 881px; height: 100px;"><thead><tr><th style="padding: 8px 14px; background-color: rgb(250, 250, 250); border: 1px solid rgb(192, 192, 192); border-collapse: collapse; min-width: 50px;">变量定义</th><th style="padding: 8px 14px; background-color: rgb(250, 250, 250); border: 1px solid rgb(192, 192, 192); border-collapse: collapse; min-width: 50px;">必须变量</th><th style="padding: 8px 14px; background-color: rgb(250, 250, 250); border: 1px solid rgb(192, 192, 192); border-collapse: collapse; min-width: 50px;">可选变量</th><th style="padding: 8px 14px; background-color: rgb(250, 250, 250); border: 1px solid rgb(192, 192, 192); border-collapse: collapse; min-width: 50px;">变量分隔符</th></tr></thead><tbody><tr><td style="padding: 8px 14px; border-color: rgb(192, 192, 192); border-collapse: collapse; min-width: 50px; text-align: center;"><span style="font-size: 16px;">普通变量</span></td><td style="padding: 8px 14px; border-color: rgb(192, 192, 192); border-collapse: collapse; min-width: 50px; text-align: center;"><span style="font-size: 16px;"><code>:name</code></span></td><td style="padding: 8px 14px; border-color: rgb(192, 192, 192); border-collapse: collapse; min-width: 50px; text-align: center;"><span style="font-size: 16px;"><code>[:name]</code></span></td><td style="padding: 8px 14px; border-color: rgb(192, 192, 192); border-collapse: collapse; min-width: 50px; text-align: center;"><span style="font-size: 16px;"><code>/</code></span></td></tr><tr><td style="padding: 8px 14px; border-color: rgb(192, 192, 192); border-collapse: collapse; min-width: 50px; text-align: center;"><span style="font-size: 16px;">组合变量</span></td><td style="padding: 8px 14px; border-color: rgb(192, 192, 192); border-collapse: collapse; min-width: 50px; text-align: center;"><span style="font-size: 16px;"><code>&lt;name&gt;</code></span></td><td style="padding: 8px 14px; border-color: rgb(192, 192, 192); border-collapse: collapse; min-width: 50px; text-align: center;"><span style="font-size: 16px;"><code>&lt;name?&gt;</code></span></td><td style="padding: 8px 14px; border-color: rgb(192, 192, 192); border-collapse: collapse; min-width: 50px; text-align: center;"><span style="font-size: 16px;">实际URL分隔符</span></td></tr></tbody></table><p style="margin: 10px auto;"><span style="font-size: 18px;"><strong>4.5、完整匹配</strong></span></p><p style="margin: 10px auto;"><span style="font-size: 16px;">规则匹配检测的时候只是对URL从头开始匹配,只要URL地址包含了定义的路由规则就会匹配成功,如果希望完全匹配,可以在路由表达式最后使用<code>$</code>符号,例如:</span></p><div class="cnblogs_code" style="margin-top: 5px; margin-bottom: 5px; padding: 5px; background-color: rgb(245, 245, 245); border: 1px solid rgb(204, 204, 204); overflow: auto; color: rgb(0, 0, 0); font-family: &quot;Courier New&quot; !important; font-size: 12px !important;"><pre style="white-space: pre-wrap; font-family: &quot;Courier New&quot; !important;">//正确的写法<br>'new/:cate$'=&gt; 'News/category',//路径:<code class="ಠhighlight-container hljs less"><span class="hljs-attribute" style="color: rgb(163, 21, 21); font-family: &quot;Courier New&quot; !important; line-height: 1.5 !important;">http</span><span class="token punctuation" style="font-family: &quot;Courier New&quot; !important; line-height: 1.5 !important;">:<span class="token operator" style="line-height: 1.5 !important;"><span class="hljs-comment" style="color: rgb(0, 128, 0); line-height: 1.5 !important;">/</span><span class="token operator" style="line-height: 1.5 !important;"><span class="hljs-comment" style="color: rgb(0, 128, 0); line-height: 1.5 !important;">/serverName</span><span class="token operator" style="line-height: 1.5 !important;"><span class="hljs-comment" style="color: rgb(0, 128, 0); line-height: 1.5 !important;">/index</span><span class="token punctuation" style="line-height: 1.5 !important;"><span class="hljs-comment" style="color: rgb(0, 128, 0); line-height: 1.5 !important;">.php</span><span class="token operator" style="line-height: 1.5 !important;"><span class="hljs-comment" style="color: rgb(0, 128, 0); line-height: 1.5 !important;">/</span><span class="token keyword" style="line-height: 1.5 !important;"><span class="hljs-comment" style="color: rgb(0, 128, 0); line-height: 1.5 !important;">new</span><span class="token operator" style="line-height: 1.5 !important;"><span class="hljs-comment" style="color: rgb(0, 128, 0); line-height: 1.5 !important;">/info</span><br></span></span></span></span></span></span></span></span></code><code class="ಠhighlight-container hljs dart"><span class="token string" style="font-family: &quot;Courier New&quot; !important; line-height: 1.5 !important;"><span class="hljs-string" style="color: rgb(163, 21, 21); line-height: 1.5 !important;">'hello/[:name]$'</span> <span class="token operator" style="line-height: 1.5 !important;">=<span class="token operator" style="line-height: 1.5 !important;">&gt; <span class="token string" style="line-height: 1.5 !important;"><span class="hljs-string" style="color: rgb(163, 21, 21); line-height: 1.5 !important;">'index/hello'</span><span class="token punctuation" style="line-height: 1.5 !important;">,</span></span></span></span></span></code></pre><pre style="white-space: pre-wrap; font-family: &quot;Courier New&quot; !important;">//错误的写法<br>'url/[:id]/[:name$]' =&gt; 'index/index2/url',</pre></div><p style="margin: 10px auto;">&nbsp;</p></div></div><p></p>
页: [1]
查看完整版本: ThinkPHP5—路由(route)详解