<article><h1>Laravel 的视图功能</h1><ul><li><a href="#creating-views">创建视图</a></li><li><a href="#passing-data-to-views">传递数据到视图</a><ul><li><a href="#sharing-data-with-all-views">共享数据给所有视图</a></li></ul></li><li><a href="#view-composers">视图合成器</a></li></ul><p><a name="creating-views"></a></p><h2><a href="#creating-views">创建视图</a></h2><p>视图的用途是用来存放应用程序中 HTML 内容,并且能够将你的控制器层(或应用逻辑层)与展现层分开。视图文件目录为 <code class=" language-php">resources<span class="token operator">/</span>views</code> ,示例视图如下:</p><pre class=" language-php"><code class=" language-php"><span class="token markup"><span class="token comment" spellcheck="true"><!-- 此视图文件位置:resources/views/greeting.blade.php --></span></span>
<span class="token markup"><span class="token tag"><span class="token tag"><span class="token punctuation"><</span>html</span><span class="token punctuation">></span></span></span>
<span class="token markup"><span class="token tag"><span class="token tag"><span class="token punctuation"><</span>body</span><span class="token punctuation">></span></span></span>
<span class="token markup"><span class="token tag"><span class="token tag"><span class="token punctuation"><</span>h1</span><span class="token punctuation">></span></span></span>Hello<span class="token punctuation">,</span> <span class="token punctuation">{</span><span class="token punctuation">{</span> <span class="token variable">$name</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token markup"><span class="token tag"><span class="token tag"><span class="token punctuation"></</span>h1</span><span class="token punctuation">></span></span></span>
<span class="token markup"><span class="token tag"><span class="token tag"><span class="token punctuation"></</span>body</span><span class="token punctuation">></span></span></span>
<span class="token markup"><span class="token tag"><span class="token tag"><span class="token punctuation"></</span>html</span><span class="token punctuation">></span></span></span></code></pre><p>上述视图文件位置为 <code class=" language-php">resources<span class="token operator">/</span>views<span class="token operator">/</span>greeting<span class="token punctuation">.</span>blade<span class="token punctuation">.</span>php</code> ,我们可以通过全局函数 <code class=" language-php">view</code> 来使用这个视图,如下:</p><pre class=" language-php"><code class=" language-php"><span class="token scope">Route<span class="token punctuation">::</span></span><span class="token function">get<span class="token punctuation">(</span></span><span class="token string">'/'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token keyword">return</span> <span class="token function">view<span class="token punctuation">(</span></span><span class="token string">'greeting'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string">'name'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token string">'James'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><p>如你所见,<code class=" language-php">view</code> 函数中,第一个参数是 <code class=" language-php">resources<span class="token operator">/</span>views</code> 目录中视图文件的文件名,第二个参数是一个数组,数组中的数据可以直接在视图文件中使用。在上面示例中,我们将 <code class=" language-php">name</code> 变量传递到了视图中,并在视图中使用 <a href="/docs/5.4/blade">Blade 模板语言</a> 打印出来。</p><p>当然,视图文件也可能存放在 <code class=" language-php">resources<span class="token operator">/</span>views</code> 的子目录中,你可以使用英文句点 <code class=" language-php"><span class="token punctuation">.</span></code> 来引用深层子目录中的视图文件。例如,一个视图的位置为 <code class=" language-php">resources<span class="token operator">/</span>views<span class="token operator">/</span>admin<span class="token operator">/</span>profile<span class="token punctuation">.</span>blade<span class="token punctuation">.</span>php</code> ,使用示例如下:</p><pre class=" language-php"><code class=" language-php"><span class="token keyword">return</span> <span class="token function">view<span class="token punctuation">(</span></span><span class="token string">'admin.profile'</span><span class="token punctuation">,</span> <span class="token variable">$data</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><h4>判断视图文件是否存在</h4><p>如果需要判断一个视图文件是否存在,你可以使用 <code class=" language-php">View</code> Facade 上的 <code class=" language-php">exists</code> 方法来判定,如果视图文件存在,则返回值为 <code class=" language-php"><span class="token boolean">true</span></code> :</p><pre class=" language-php"><code class=" language-php"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>View</span><span class="token punctuation">;</span>
<span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token scope">View<span class="token punctuation">::</span></span><span class="token function">exists<span class="token punctuation">(</span></span><span class="token string">'emails.customer'</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token comment" spellcheck="true"> //
</span><span class="token punctuation">}</span></code></pre><p><a name="passing-data-to-views"></a></p><h2><a href="#passing-data-to-views">传递数据到视图</a></h2><p>如上述例子中,你可以使用数组将数据传递到视图文件:</p><pre class=" language-php"><code class=" language-php"><span class="token keyword">return</span> <span class="token function">view<span class="token punctuation">(</span></span><span class="token string">'greetings'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string">'name'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token string">'Victoria'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><p>当使用上面方式传递数据时,第二个参数( <code class=" language-php"><span class="token variable">$data</span></code> )必须是键值对数组(关联数组)。在视图文件中,你可以通过对应的关键字( <code class=" language-php"><span class="token variable">$key</span></code> )取用相应的数据值,例如 <code class=" language-php"><span class="token php"><span class="token delimiter"><?php</span> <span class="token keyword">echo</span> <span class="token variable">$key</span><span class="token punctuation">;</span> <span class="token delimiter">?></span></span></code>。如果只需要传递特定数据而非一个臃肿的数组到视图文件,可以使用 <code class=" language-php">with</code> 辅助函数,示例如下:</p><pre class=" language-php"><code class=" language-php"><span class="token keyword">return</span> <span class="token function">view<span class="token punctuation">(</span></span><span class="token string">'greeting'</span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">with<span class="token punctuation">(</span></span><span class="token string">'name'</span><span class="token punctuation">,</span> <span class="token string">'Victoria'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><p><a name="sharing-data-with-all-views"></a></p><h4>把数据共享给所有视图</h4><p>有时候可能需要共享特定的数据给应用程序中所有的视图,那这时候你需要 <code class=" language-php">View</code> Facade 的 <code class=" language-php">share</code> 方法。通常需要将所有 <code class=" language-php">share</code> 方法的调用代码放到 <a href="/docs/5.4/providers">服务提供者</a> 的 <code class=" language-php">boot</code> 方法中,此时你可以选择使用 <code class=" language-php">AppServiceProvider</code> 或创建独立的 <a href="/docs/5.4/providers">服务提供者</a> 。示例代码如下:</p><pre class=" language-php"><code class=" language-php"><span class="token delimiter"><?php</span>
<span class="token keyword">namespace</span> <span class="token package">App<span class="token punctuation">\</span>Providers</span><span class="token punctuation">;</span>
<span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>View</span><span class="token punctuation">;</span>
<span class="token keyword">class</span> <span class="token class-name">AppServiceProvider</span> <span class="token keyword">extends</span> <span class="token class-name">ServiceProvider</span>
<span class="token punctuation">{</span>
<span class="token comment" spellcheck="true">/**
* Bootstrap any application services.
*
* @return void
*/</span>
<span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function">boot<span class="token punctuation">(</span></span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
<span class="token scope">View<span class="token punctuation">::</span></span><span class="token function">share<span class="token punctuation">(</span></span><span class="token string">'key'</span><span class="token punctuation">,</span> <span class="token string">'value'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token comment" spellcheck="true">/**
* Register the service provider.
*
* @return void
*/</span>
<span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function">register<span class="token punctuation">(</span></span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
<span class="token comment" spellcheck="true"> //
</span> <span class="token punctuation">}</span>
<span class="token punctuation">}</span></code></pre><p><a name="view-composers"></a></p><h2><a href="#view-composers">视图合成器</a></h2><p>视图合成器是在视图渲染时调用的一些回调或者类方法。如果你需要在某些视图渲染时绑定一些数据上去,那么视图合成器就是你的的不二之选,另外他还可以帮你将这些绑定逻辑整理到特定的位置。</p><p>下面例子中,我们会在一个 <a href="/docs/5.4/providers">服务提供者</a> 中注册一些视图合成器。同时使用 <code class=" language-php">View</code> Facade 来访问 <code class=" language-php">Illuminate\<span class="token package">Contracts<span class="token punctuation">\</span>View<span class="token punctuation">\</span>Factory</span></code> contract 的底层实现。注意:Laravel 没有存放视图合成器的默认目录,但你可以根据自己的喜好来重新组织,例如:<code class=" language-php">App\<span class="token package">Http<span class="token punctuation">\</span>ViewComposers</span></code>。</p><pre class=" language-php"><code class=" language-php"><span class="token delimiter"><?php</span>
<span class="token keyword">namespace</span> <span class="token package">App<span class="token punctuation">\</span>Providers</span><span class="token punctuation">;</span>
<span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>View</span><span class="token punctuation">;</span>
<span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>ServiceProvider</span><span class="token punctuation">;</span>
<span class="token keyword">class</span> <span class="token class-name">ComposerServiceProvider</span> <span class="token keyword">extends</span> <span class="token class-name">ServiceProvider</span>
<span class="token punctuation">{</span>
<span class="token comment" spellcheck="true">/**
* Register bindings in the container.
*
* @return void
*/</span>
<span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function">boot<span class="token punctuation">(</span></span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
<span class="token comment" spellcheck="true"> // Using class based composers...
</span> <span class="token scope">View<span class="token punctuation">::</span></span><span class="token function">composer<span class="token punctuation">(</span></span>
<span class="token string">'profile'</span><span class="token punctuation">,</span> <span class="token string">'App\Http\ViewComposers\ProfileComposer'</span>
<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true"> // Using Closure based composers...
</span> <span class="token scope">View<span class="token punctuation">::</span></span><span class="token function">composer<span class="token punctuation">(</span></span><span class="token string">'dashboard'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token variable">$view</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token comment" spellcheck="true"> //
</span> <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token comment" spellcheck="true">/**
* Register the service provider.
*
* @return void
*/</span>
<span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function">register<span class="token punctuation">(</span></span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
<span class="token comment" spellcheck="true"> //
</span> <span class="token punctuation">}</span>
<span class="token punctuation">}</span></code></pre><blockquote class="has-icon note"><p><div class="flag"><span class="svg"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:a="http://ns.adobe.com/AdobeSVGViewerExtensions/3.0/" version="1.1" x="0px" y="0px" width="90px" height="90px" viewBox="0 0 90 90" enable-background="new 0 0 90 90" xml:space="preserve"><path fill="#FFFFFF" d="M45 0C20.1 0 0 20.1 0 45s20.1 45 45 45 45-20.1 45-45S69.9 0 45 0zM45 74.5c-3.6 0-6.5-2.9-6.5-6.5s2.9-6.5 6.5-6.5 6.5 2.9 6.5 6.5S48.6 74.5 45 74.5zM52.1 23.9l-2.5 29.6c0 2.5-2.1 4.6-4.6 4.6 -2.5 0-4.6-2.1-4.6-4.6l-2.5-29.6c-0.1-0.4-0.1-0.7-0.1-1.1 0-4 3.2-7.2 7.2-7.2 4 0 7.2 3.2 7.2 7.2C52.2 23.1 52.2 23.5 52.1 23.9z"></path></svg></span></div> 注意,如果你创建了新的一个 <a href="/docs/5.4/providers">服务提供者</a> 来存放你视图合成器的注册项,那么你需要将这个 <a href="/docs/5.4/providers">服务提供者</a> 添加到配置文件 <code class=" language-php">config<span class="token operator">/</span>app<span class="token punctuation">.</span>php</code> 的 <code class=" language-php">providers</code> 数组中。</p></blockquote><p>到此我们已经注册了上面的视图合成器,效果是每次 <code class=" language-php">profile</code> 视图渲染时,都会执行 <code class=" language-php">ProfileComposer@compose</code> 方法。那么下面我们来定义这个合成器类吧。</p><pre class=" language-php"><code class=" language-php"><span class="token delimiter"><?php</span>
<span class="token keyword">namespace</span> <span class="token package">App<span class="token punctuation">\</span>Http<span class="token punctuation">\</span>ViewComposers</span><span class="token punctuation">;</span>
<span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>View<span class="token punctuation">\</span>View</span><span class="token punctuation">;</span>
<span class="token keyword">use</span> <span class="token package">App<span class="token punctuation">\</span>Repositories<span class="token punctuation">\</span>UserRepository</span><span class="token punctuation">;</span>
<span class="token keyword">class</span> <span class="token class-name">ProfileComposer</span>
<span class="token punctuation">{</span>
<span class="token comment" spellcheck="true">/**
* The user repository implementation.
*
* @var UserRepository
*/</span>
<span class="token keyword">protected</span> <span class="token variable">$users</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">/**
* Create a new profile composer.
*
* @param UserRepository $users
* @return void
*/</span>
<span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function">__construct<span class="token punctuation">(</span></span>UserRepository <span class="token variable">$users</span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
<span class="token comment" spellcheck="true"> // Dependencies automatically resolved by service container...
</span> <span class="token this">$this</span><span class="token operator">-</span><span class="token operator">></span><span class="token property">users</span> <span class="token operator">=</span> <span class="token variable">$users</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token comment" spellcheck="true">/**
* Bind data to the view.
*
* @param View $view
* @return void
*/</span>
<span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function">compose<span class="token punctuation">(</span></span>View <span class="token variable">$view</span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
<span class="token variable">$view</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">with<span class="token punctuation">(</span></span><span class="token string">'count'</span><span class="token punctuation">,</span> <span class="token this">$this</span><span class="token operator">-</span><span class="token operator">></span><span class="token property">users</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">count<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token punctuation">}</span></code></pre><p>每当视图渲染时,该合成器的 <code class=" language-php">compose</code> 方法都会被调用,并且传入一个 <code class=" language-php">Illuminate\<span class="token package">View<span class="token punctuation">\</span>View</span></code> 实例作为参数,在这个过程中,你可以使用 <code class=" language-php">with</code> 方法绑定数据到目标视图。</p><blockquote class="has-icon tip"><p><div class="flag"><span class="svg"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:a="http://ns.adobe.com/AdobeSVGViewerExtensions/3.0/" version="1.1" x="0px" y="0px" width="56.6px" height="87.5px" viewBox="0 0 56.6 87.5" enable-background="new 0 0 56.6 87.5" xml:space="preserve"><path fill="#FFFFFF" d="M28.7 64.5c-1.4 0-2.5-1.1-2.5-2.5v-5.7 -5V41c0-1.4 1.1-2.5 2.5-2.5s2.5 1.1 2.5 2.5v10.1 5 5.8C31.2 63.4 30.1 64.5 28.7 64.5zM26.4 0.1C11.9 1 0.3 13.1 0 27.7c-0.1 7.9 3 15.2 8.2 20.4 0.5 0.5 0.8 1 1 1.7l3.1 13.1c0.3 1.1 1.3 1.9 2.4 1.9 0.3 0 0.7-0.1 1.1-0.2 1.1-0.5 1.6-1.8 1.4-3l-2-8.4 -0.4-1.8c-0.7-2.9-2-5.7-4-8 -1-1.2-2-2.5-2.7-3.9C5.8 35.3 4.7 30.3 5.4 25 6.7 14.5 15.2 6.3 25.6 5.1c13.9-1.5 25.8 9.4 25.8 23 0 4.1-1.1 7.9-2.9 11.2 -0.8 1.4-1.7 2.7-2.7 3.9 -2 2.3-3.3 5-4 8L41.4 53l-2 8.4c-0.3 1.2 0.3 2.5 1.4 3 0.3 0.2 0.7 0.2 1.1 0.2 1.1 0 2.2-0.8 2.4-1.9l3.1-13.1c0.2-0.6 0.5-1.2 1-1.7 5-5.1 8.2-12.1 8.2-19.8C56.4 12 42.8-1 26.4 0.1zM43.7 69.6c0 0.5-0.1 0.9-0.3 1.3 -0.4 0.8-0.7 1.6-0.9 2.5 -0.7 3-2 8.6-2 8.6 -1.3 3.2-4.4 5.5-7.9 5.5h-4.1H28h-0.5 -3.6c-3.5 0-6.7-2.4-7.9-5.7l-0.1-0.4 -1.8-7.8c-0.4-1.1-0.8-2.1-1.2-3.1 -0.1-0.3-0.2-0.5-0.2-0.9 0.1-1.3 1.3-2.1 2.6-2.1H41C42.4 67.5 43.6 68.2 43.7 69.6zM37.7 72.5H26.9c-4.2 0-7.2 3.9-6.3 7.9 0.6 1.3 1.8 2.1 3.2 2.1h4.1 0.5 0.5 3.6c1.4 0 2.7-0.8 3.2-2.1L37.7 72.5z"></path></svg></span></div> 所有的视图合成器都会由 <a href="/docs/5.4/container">服务容器</a> 来解析,所以你可以在合成器的构造函数中使用类型提示来注入需要的任何依赖。</p></blockquote><h4>将视图合成器附加到多个视图</h4><p>通过将目标视图文件数组作为第一个参数传入 <code class=" language-php">composer</code> 方法,你可以把一个视图合成器同时附加到多个视图。</p><pre class=" language-php"><code class=" language-php"><span class="token scope">View<span class="token punctuation">::</span></span><span class="token function">composer<span class="token punctuation">(</span></span>
<span class="token punctuation">[</span><span class="token string">'profile'</span><span class="token punctuation">,</span> <span class="token string">'dashboard'</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
<span class="token string">'App\Http\ViewComposers\MyViewComposer'</span>
<span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><p><code class=" language-php">composer</code> 方法同时也接受通配符 <code class=" language-php"><span class="token operator">*</span></code> ,可以让你将一个视图合成器一次性绑定到所有的视图:</p><pre class=" language-php"><code class=" language-php"><span class="token scope">View<span class="token punctuation">::</span></span><span class="token function">composer<span class="token punctuation">(</span></span><span class="token string">'*'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token variable">$view</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token comment" spellcheck="true"> //
</span><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><h4>视图构造器</h4><p>视图 <strong>构造器</strong> 和视图合成器非常相似。不同之处在于:视图构造器在视图实例化时执行,而视图合成器在视图渲染时执行。如下,可以使用 <code class=" language-php">creator</code> 方法来注册一个视图塑造器:</p><pre class=" language-php"><code class=" language-php"><span class="token scope">View<span class="token punctuation">::</span></span><span class="token function">creator<span class="token punctuation">(</span></span><span class="token string">'profile'</span><span class="token punctuation">,</span> <span class="token string">'App\Http\ViewCreators\ProfileCreator'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><h2>译者署名</h2><table><thead><tr><th>用户名</th><th>头像</th><th>职能</th><th>签名</th></tr></thead><tbody><tr><td><a href="https://github.com/cmzz">@zyxcba</a></td><td><img class="avatar-66 rm-style" src="https://avatars3.githubusercontent.com/u/6111715?v=3&s=100"></td><td>翻译</td><td><a href="http://wewx.cn">最新最好玩的微信小程序</a> - WxStore 开源微信小程序商城</td></tr></tbody></table></article>
- 入门指南
- 安装
- 配置信息
- 文件夹结构
- 请求周期
- 开发环境部署
- Valet
- Homestead
- 核心概念
- 服务提供者
- Facades
- Contracts
- 服务容器
- HTTP 层
- 路由
- 中间件
- CSRF 保护
- 控制器
- 请求
- 响应
- 视图
- Session
- 表单验证
- 前端
- Blade 模板
- 本地化
- 前端指南
- 编辑资源 Mix
- 安全
- API 认证
- 用户认证
- 用户授权
- 加密解密
- 哈希
- 重置密码
- 数据库
- 快速入门
- 查询构造器
- 分页
- 数据库迁移
- Redis
- 数据填充
- Eloquent ORM
- Eloquent ORM快速入门
- 模型关联
- Eloquent 集合
- 修改器
- 序列化
- 综合话题
- Artisan 命令行
- 广播系统
- 缓存系统
- 集合
- 错误与日志
- 事件系统
- 文件存储
- 辅助函数
- 邮件发送
- 消息通知
- 扩展包开发
- 队列
- 任务调度