🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
# 路由 ``` <pre class="calibre29">``` Route<span class="token1">:</span><span class="token1">:</span><span class="token2">get</span><span class="token1">(</span><span class="token4">'/'</span><span class="token1">,</span> <span class="token2">function</span><span class="token3"> </span><span class="token1">(</span><span class="token1">)</span> <span class="token1">{</span> <span class="token3">returnview</span><span class="token1">(</span><span class="token4">'welcome'</span><span class="token1">)</span><span class="token1">;</span> <span class="token1">}</span><span class="token1">)</span><span class="token1">;</span> <span class="token6">// 测试路由</span> Route<span class="token1">:</span><span class="token1">:</span><span class="token2">get</span><span class="token1">(</span><span class="token4">'test1'</span><span class="token1">,</span> <span class="token2">function</span><span class="token3"> </span><span class="token1">(</span><span class="token1">)</span> <span class="token1">{</span> <span class="token2">return</span><span class="token4">'test1'</span><span class="token1">;</span> <span class="token1">}</span><span class="token1">)</span><span class="token1">;</span> Route<span class="token1">:</span><span class="token1">:</span><span class="token3">post</span><span class="token1">(</span><span class="token4">'test2'</span><span class="token1">,</span> <span class="token2">function</span><span class="token3"> </span><span class="token1">(</span><span class="token1">)</span> <span class="token1">{</span> <span class="token2">return</span><span class="token4">'test2'</span><span class="token1">;</span> <span class="token6">//需要加入免鉴权 否则会出现419错误</span> <span class="token1">}</span><span class="token1">)</span><span class="token1">;</span> <span class="token6">//指定条件匹配</span> Route<span class="token1">:</span><span class="token1">:</span><span class="token3">match</span><span class="token1">(</span>\<span class="token1">[</span><span class="token4">'post'</span><span class="token1">,</span><span class="token4">'get'</span>\<span class="token1">]</span><span class="token1">,</span> <span class="token4">'test3'</span><span class="token1">,</span> <span class="token2">function</span><span class="token3"> </span><span class="token1">(</span><span class="token1">)</span> <span class="token1">{</span> <span class="token2">return</span><span class="token4">'条件匹配'</span><span class="token1">;</span> <span class="token1">}</span><span class="token1">)</span><span class="token1">;</span> <span class="token6">//响应所有匹配</span> Route<span class="token1">:</span><span class="token1">:</span><span class="token3">any</span><span class="token1">(</span><span class="token4">'test4'</span><span class="token1">,</span> <span class="token2">function</span><span class="token3"> </span><span class="token1">(</span><span class="token1">)</span> <span class="token1">{</span> <span class="token2">return</span><span class="token4">'响应所有路由'</span><span class="token1">;</span> <span class="token1">}</span><span class="token1">)</span><span class="token1">;</span> ``` ``` ``` <pre class="calibre13">``` <span class="token6">//路由参数(参数必填)</span> Route<span class="token1">:</span><span class="token1">:</span><span class="token2">get</span><span class="token1">(</span><span class="token4">'test1/{id}'</span><span class="token1">,</span> <span class="token2">function</span><span class="token3"> </span><span class="token1">(</span>$id<span class="token1">)</span> <span class="token1">{</span> <span class="token2">return</span><span class="token4">'USER-ID-'</span><span class="token1">.</span>$id<span class="token1">;</span> <span class="token1">}</span><span class="token1">)</span><span class="token1">;</span> <span class="token6">//路由参数可选(如果不设置默认值 $name=null)</span> Route<span class="token1">:</span><span class="token1">:</span><span class="token2">get</span><span class="token1">(</span><span class="token4">'test2/{name?}'</span><span class="token1">,</span> <span class="token2">function</span><span class="token3"> </span><span class="token1">(</span>$name \<span class="token">=</span><span class="token4">'moren'</span><span class="token1">)</span> <span class="token1">{</span> <span class="token2">return</span><span class="token4">'USER-name-'</span><span class="token1">.</span>$name<span class="token1">;</span> <span class="token1">}</span><span class="token1">)</span><span class="token1">;</span> <span class="token6">//路由正则匹配</span> Route<span class="token1">:</span><span class="token1">:</span><span class="token2">get</span><span class="token1">(</span><span class="token4">'test3/{id}/{name?}'</span><span class="token1">,</span> <span class="token2">function</span><span class="token3"> </span><span class="token1">(</span>$id<span class="token1">,</span> $name \<span class="token">=</span><span class="token4">'moren'</span><span class="token1">)</span> <span class="token1">{</span> <span class="token2">return</span><span class="token4">'user-id-'</span><span class="token1">.</span>$id<span class="token1">.</span><span class="token4">'-name-'</span><span class="token1">.</span>$name<span class="token1">;</span> <span class="token1">}</span><span class="token1">)</span>\<span class="token">-</span><span class="token">></span><span class="token3">where</span><span class="token1">(</span>\<span class="token1">[</span><span class="token4">'id'</span>\<span class="token">=></span><span class="token4">'\[0-9\]+'</span><span class="token1">,</span><span class="token4">'name'</span>\<span class="token">=></span><span class="token4">'\[A-Za-z\]+'</span>\<span class="token1">]</span><span class="token1">)</span><span class="token1">;</span> <span class="token6">//路由别名 如果更换了url 路由和模板是不需要更改的</span> Route<span class="token1">:</span><span class="token1">:</span><span class="token2">get</span><span class="token1">(</span><span class="token4">'user/center'</span><span class="token1">,</span> \<span class="token1">[</span><span class="token4">'as'</span>\<span class="token">=></span><span class="token4">'center'</span><span class="token1">,</span><span class="token2">function</span><span class="token3"> </span><span class="token1">(</span><span class="token1">)</span> <span class="token1">{</span> <span class="token3">returnroute</span><span class="token1">(</span><span class="token4">'center'</span><span class="token1">)</span><span class="token1">;</span> <span class="token1">}</span>\<span class="token1">]</span><span class="token1">)</span><span class="token1">;</span> <span class="token6">//路由群组 在群里里面的路由全部增加了前缀</span> <span class="token6">//访问的url http://la.com/member/member http://la.com/member/user/6</span> Route<span class="token1">:</span><span class="token1">:</span><span class="token3">group</span><span class="token1">(</span>\<span class="token1">[</span><span class="token4">'prefix'</span>\<span class="token">=></span><span class="token4">'member'</span>\<span class="token1">]</span><span class="token1">,</span> <span class="token2">function</span><span class="token3"> </span><span class="token1">(</span><span class="token1">)</span> <span class="token1">{</span> Route<span class="token1">:</span><span class="token1">:</span><span class="token2">get</span><span class="token1">(</span><span class="token4">'/user/{id}'</span><span class="token1">,</span> <span class="token2">function</span><span class="token3"> </span><span class="token1">(</span>$id<span class="token1">)</span> <span class="token1">{</span> <span class="token2">return</span><span class="token4">'user-id'</span><span class="token1">.</span>$id<span class="token1">;</span> <span class="token1">}</span><span class="token1">)</span><span class="token1">;</span> Route<span class="token1">:</span><span class="token1">:</span><span class="token2">get</span><span class="token1">(</span><span class="token4">'/member'</span><span class="token1">,</span> <span class="token2">function</span><span class="token3"> </span><span class="token1">(</span><span class="token1">)</span> <span class="token1">{</span> <span class="token2">return</span><span class="token4">'member'</span><span class="token1">;</span> <span class="token1">}</span><span class="token1">)</span><span class="token1">;</span> <span class="token1">}</span><span class="token1">)</span><span class="token1">;</span> <span class="token6">//路由中输出视图</span> Route<span class="token1">:</span><span class="token1">:</span><span class="token2">get</span><span class="token1">(</span><span class="token4">'/view'</span><span class="token1">,</span> <span class="token2">function</span><span class="token3"> </span><span class="token1">(</span><span class="token1">)</span> <span class="token1">{</span> <span class="token3">returnview</span><span class="token1">(</span><span class="token4">'welcome'</span><span class="token1">)</span><span class="token1">;</span> <span class="token1">}</span><span class="token1">)</span><span class="token1">;</span> ``` ``` # 创建控制器 ``` <pre class="calibre14">``` php artisan make<span class="token1">:</span>controller TestController ``` ```