ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
# 路由 - 路径routes/web.php是网站,视图路径resources/views ``` <pre class="calibre14">``` Route<span class="token2">:</span><span class="token2">:</span><span class="token1">get</span><span class="token2">(</span><span class="token4">'/'</span><span class="token2">,</span> <span class="token5">function</span> <span class="token2">(</span><span class="token2">)</span> <span class="token2">{</span> <span class="token6">//首页路由</span> <span class="token5">return</span> <span class="token1">view</span><span class="token2">(</span><span class="token4">'welcome'</span><span class="token2">)</span><span class="token2">;</span> <span class="token2">}</span><span class="token2">)</span><span class="token2">;</span> Route<span class="token2">:</span><span class="token2">:</span><span class="token1">get</span><span class="token2">(</span><span class="token4">'about'</span><span class="token2">,</span> <span class="token5">function</span> <span class="token2">(</span><span class="token2">)</span> <span class="token2">{</span> <span class="token6">//关于我们</span> <span class="token5">return</span> <span class="token1">view</span><span class="token2">(</span><span class="token4">'about'</span><span class="token2">)</span><span class="token2">;</span> <span class="token2">}</span><span class="token2">)</span><span class="token2">;</span> Route<span class="token2">:</span><span class="token2">:</span><span class="token1">post</span><span class="token2">(</span><span class="token4">'/'</span><span class="token2">,</span> <span class="token5">function</span> <span class="token2">(</span><span class="token2">)</span> <span class="token2">{</span><span class="token2">}</span><span class="token2">)</span><span class="token2">;</span> Route<span class="token2">:</span><span class="token2">:</span><span class="token1">put</span><span class="token2">(</span><span class="token4">'/'</span><span class="token2">,</span> <span class="token5">function</span> <span class="token2">(</span><span class="token2">)</span> <span class="token2">{</span><span class="token2">}</span><span class="token2">)</span><span class="token2">;</span> Route<span class="token2">:</span><span class="token2">:</span><span class="token1">delete</span><span class="token2">(</span><span class="token4">'/'</span><span class="token2">,</span> <span class="token5">function</span> <span class="token2">(</span><span class="token2">)</span> <span class="token2">{</span><span class="token2">}</span><span class="token2">)</span><span class="token2">;</span> Route<span class="token2">:</span><span class="token2">:</span><span class="token1">match</span><span class="token2">(</span><span class="token2">[</span><span class="token4">'get'</span><span class="token2">,</span> <span class="token4">'post'</span><span class="token2">]</span><span class="token2">,</span> <span class="token4">'/'</span><span class="token2">,</span> <span class="token5">function</span> <span class="token2">(</span><span class="token2">)</span> <span class="token2">{</span><span class="token2">}</span><span class="token2">)</span><span class="token2">;</span> Route<span class="token2">:</span><span class="token2">:</span><span class="token1">get</span><span class="token2">(</span><span class="token4">'/'</span><span class="token2">,</span> <span class="token4">'WelcomeController@index'</span><span class="token2">)</span><span class="token2">;</span><span class="token6">//App\Http\Controllers\WelcomeController 控制器的 index 方法进行处理</span> Route<span class="token2">:</span><span class="token2">:</span><span class="token1">get</span><span class="token2">(</span><span class="token4">'user/{id}'</span><span class="token2">,</span> <span class="token5">function</span> <span class="token2">(</span>$id<span class="token2">)</span> <span class="token2">{</span><span class="token6">//路由参数</span> <span class="token5">return</span> <span class="token4">"用户ID: "</span> <span class="token2">.</span> $id<span class="token2">;</span> <span class="token2">}</span><span class="token2">)</span><span class="token2">;</span> Route<span class="token2">:</span><span class="token2">:</span><span class="token1">middleware</span><span class="token2">(</span><span class="token4">'auth'</span><span class="token2">)</span><span class="token">-</span><span class="token">></span><span class="token1">group</span><span class="token2">(</span><span class="token5">function</span> <span class="token2">(</span><span class="token2">)</span> <span class="token2">{</span><span class="token6">//中间件与分组</span> Route<span class="token2">:</span><span class="token2">:</span><span class="token1">get</span><span class="token2">(</span><span class="token4">'dashboard'</span><span class="token2">,</span> <span class="token5">function</span> <span class="token2">(</span><span class="token2">)</span> <span class="token2">{</span> <span class="token5">return</span> <span class="token1">view</span><span class="token2">(</span><span class="token4">'dashboard'</span><span class="token2">)</span><span class="token2">;</span> <span class="token2">}</span><span class="token2">)</span><span class="token2">;</span> Route<span class="token2">:</span><span class="token2">:</span><span class="token1">get</span><span class="token2">(</span><span class="token4">'account'</span><span class="token2">,</span> <span class="token5">function</span> <span class="token2">(</span><span class="token2">)</span> <span class="token2">{</span> <span class="token5">return</span> <span class="token1">view</span><span class="token2">(</span><span class="token4">'account'</span><span class="token2">)</span><span class="token2">;</span> <span class="token2">}</span><span class="token2">)</span><span class="token2">;</span> <span class="token2">}</span><span class="token2">)</span><span class="token2">;</span> Route<span class="token2">:</span><span class="token2">:</span><span class="token1">get</span><span class="token2">(</span><span class="token4">'/task'</span><span class="token2">,</span> <span class="token4">'TaskController@home'</span><span class="token2">)</span><span class="token2">;</span>指向控制器的路由 <span class="token5">return</span> <span class="token1">view</span><span class="token2">(</span><span class="token4">'home'</span><span class="token2">,</span> <span class="token2">[</span><span class="token4">'tasks'</span> <span class="token">=</span><span class="token">></span> Task<span class="token2">:</span><span class="token1">all</span><span class="token2">(</span><span class="token2">)</span><span class="token2">]</span><span class="token2">)</span><span class="token2">;</span> <span class="token6">//视图传递数组,Task是控制器,也可以是数组$tasks</span> <span class="token5">return</span> <span class="token1">view</span><span class="token2">(</span><span class="token4">'home'</span><span class="token2">)</span><span class="token">-</span><span class="token">></span><span class="token1">share</span><span class="token2">(</span><span class="token4">'siteName'</span><span class="token2">,</span> <span class="token4">'Laravel学院'</span><span class="token2">)</span><span class="token2">;</span><span class="token6">//共享变量</span> 视图中 <span class="token"><</span>a href<span class="token">=</span><span class="token4">"{{ url('/') }}"</span><span class="token">></span> 路由命令与调用 Route<span class="token2">:</span><span class="token2">:</span><span class="token1">get</span><span class="token2">(</span><span class="token4">'user/{id?}'</span><span class="token2">,</span> <span class="token5">function</span> <span class="token2">(</span>$id <span class="token">=</span> <span class="token3">1</span><span class="token2">)</span> <span class="token2">{</span> <span class="token5">return</span> <span class="token4">"用户ID: "</span> <span class="token2">.</span> $id<span class="token2">;</span> <span class="token2">}</span><span class="token2">)</span><span class="token">-</span><span class="token">></span><span class="token1">name</span><span class="token2">(</span><span class="token4">'user.profile'</span><span class="token2">)</span><span class="token2">;</span> <span class="token"><</span>a href<span class="token">=</span><span class="token4">"{{ route('user.profile', [100]) }}"</span><span class="token">></span> ``` ``` - blade模板 ``` <pre class="calibre16">``` <span class="token2">{</span><span class="token2">{</span> <span class="token2">}</span><span class="token2">}</span>渲染变量 @<span class="token2">{</span><span class="token2">{</span> <span class="token2">}</span><span class="token2">}</span> 跳过js 通过 <span class="token2">{</span><span class="token">!</span><span class="token">!</span> <span class="token">!</span><span class="token">!</span><span class="token2">}</span> 渲染原生 HTML 代码 <span class="token2">{</span><span class="token2">{</span><span class="token">--</span> 注释内容 <span class="token">--</span><span class="token2">}</span><span class="token2">}</span> @<span class="token5">if</span>、@<span class="token5">else</span>、@elseif @isset、@empty @<span class="token5">for</span>、@foreach 和 @<span class="token5">while</span> @include ``` ```