<article><h1>Laravel 服务容器解析</h1><ul><li><a href="#introduction">简介</a></li><li><a href="#binding">绑定</a><ul><li><a href="#binding-basics">绑定基础</a></li><li><a href="#binding-interfaces-to-implementations">绑定接口至实现</a></li><li><a href="#contextual-binding">情境绑定</a></li><li><a href="#tagging">标记</a></li></ul></li><li><a href="#resolving">解析</a><ul><li><a href="#the-make-method">Make 方法</a></li><li><a href="#automatic-injection">自动注入</a></li></ul></li><li><a href="#container-events">容器事件</a></li></ul><p><a name="introduction"></a></p><h2><a href="#introduction">简介</a></h2><p>Laravel 服务容器是管理类依赖和运行依赖注入的有力工具。依赖注入是一个花俏的名词,它实质上是指:类的依赖通过构造器或在某些情况下通过「setter」方法进行「注入」。</p><p>来看一个简单的例子:</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>Controllers</span><span class="token punctuation">;</span>
<span class="token keyword">use</span> <span class="token package">App<span class="token punctuation">\</span>User</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">use</span> <span class="token package">App<span class="token punctuation">\</span>Http<span class="token punctuation">\</span>Controllers<span class="token punctuation">\</span>Controller</span><span class="token punctuation">;</span>
<span class="token keyword">class</span> <span class="token class-name">UserController</span> <span class="token keyword">extends</span> <span class="token class-name">Controller</span>
<span class="token punctuation">{</span>
<span class="token comment" spellcheck="true">/**
* User Repository 的实现。
*
* @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">/**
* 创建新的控制器实例。
*
* @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 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">/**
* 显示指定用户的详细信息。
*
* @param int $id
* @return Response
*/</span>
<span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function">show<span class="token punctuation">(</span></span><span class="token variable">$id</span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
<span class="token variable">$user</span> <span class="token operator">=</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">find<span class="token punctuation">(</span></span><span class="token variable">$id</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">'user.profile'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string">'user'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token variable">$user</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">UserController</code> 需要从数据源中获取 users 。因此,我们要 <strong>注入</strong> 可以获取 users 的服务。在这种情况下, <code class=" language-php">UserRepository</code> 可能是通过使用 <a href="/docs/5.4/eloquent">Eloquent</a> 来从数据库中获取 user 信息。因为 <code class=" language-php">UserRepository</code> 是通过注入获取,所以我们可以容易地切换为其他实现。当测试应用程序时,我们还可以轻松地 「mock」 ,或创建假的 <code class=" language-php">UserRepository</code> 实例。</p><p>在构建强大的应用程序,和为 Laravel 核心贡献代码时,必须深入理解 Laravel 的服务容器。</p><p><a name="binding"></a></p><h2><a href="#binding">绑定</a></h2><p><a name="binding-basics"></a></p><h3>绑定基础</h3><p>几乎所有服务容器的绑定都是在 <a href="/docs/5.4/providers">服务提供者</a> 中进行的,所以下面的例子将示范在该情景中使用容器。</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> 但是,如果类没有依赖任何接口,那么就没有必要将类绑定到容器中了。容器绑定时,并不需要指定如何构建这些类,因为容器中会通过 PHP 的反射自动解析对象。</p></blockquote><h4>简单绑定</h4><p>在服务提供者中,你经常可以通过 <code class=" language-php"><span class="token this">$this</span><span class="token operator">-</span><span class="token operator">></span><span class="token property">app</span></code> 属性访问容器。我们可以通过 <code class=" language-php">bind</code> 方法注册一个绑定,通过传递注册类或接口的名称、及返回该实例的 <code class=" language-php">Closure</code> 作为参数:</p><pre class=" language-php"><code class=" language-php"><span class="token this">$this</span><span class="token operator">-</span><span class="token operator">></span><span class="token property">app</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">bind<span class="token punctuation">(</span></span><span class="token string">'HelpSpot\API'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token variable">$app</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token keyword">return</span> <span class="token keyword">new</span> <span class="token class-name">HelpSpot<span class="token punctuation">\</span>API</span><span class="token punctuation">(</span><span class="token variable">$app</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">make<span class="token punctuation">(</span></span><span class="token string">'HttpClient'</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>注意,我们将获得的容器本身作为参数传递到解析器中,这样就可以使用容器来解决绑定对象对容器的子依赖。</p><h4>绑定一个单例</h4><p>通过 <code class=" language-php">singleton</code> 方法可以绑定一个只会被解析一次的类或接口到容器中。且后面的调用都会从容器中返回相同的实例:</p><pre class=" language-php"><code class=" language-php"><span class="token this">$this</span><span class="token operator">-</span><span class="token operator">></span><span class="token property">app</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">singleton<span class="token punctuation">(</span></span><span class="token string">'HelpSpot\API'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token variable">$app</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token keyword">return</span> <span class="token keyword">new</span> <span class="token class-name">HelpSpot<span class="token punctuation">\</span>API</span><span class="token punctuation">(</span><span class="token variable">$app</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">make<span class="token punctuation">(</span></span><span class="token string">'HttpClient'</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><h4>绑定实例</h4><p>你也可以使用 <code class=" language-php">instance</code> 方法绑定一个已经存在的对象至容器中。后面的调用都会从容器中返回指定的实例:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$api</span> <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">HelpSpot<span class="token punctuation">\</span>API</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">HttpClient</span><span class="token punctuation">)</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">app</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">instance<span class="token punctuation">(</span></span><span class="token string">'HelpSpot\Api'</span><span class="token punctuation">,</span> <span class="token variable">$api</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><h4>绑定初始数据</h4><p>有时,你的类不仅需要注入类,还需要注入一些原始数据,如一个整数。此时,你可以容易地通过情景绑定注入需要的任何值:</p><pre class=" language-php"><code class=" language-php"><span class="token this">$this</span><span class="token operator">-</span><span class="token operator">></span><span class="token property">app</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">when<span class="token punctuation">(</span></span><span class="token string">'App\Http\Controllers\UserController'</span><span class="token punctuation">)</span>
<span class="token operator">-</span><span class="token operator">></span><span class="token function">needs<span class="token punctuation">(</span></span><span class="token string">'$variableName'</span><span class="token punctuation">)</span>
<span class="token operator">-</span><span class="token operator">></span><span class="token function">give<span class="token punctuation">(</span></span><span class="token variable">$value</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><p><a name="binding-interfaces-to-implementations"></a></p><h3>绑定接口至实现</h3><p>服务容器有一个强大的功能,就是将一个指定接口的实现绑定到接口上。例如,如果我们有一个 <code class=" language-php">EventPusher</code> 接口和一个它的实现类 <code class=" language-php">RedisEventPusher</code> 。编写完接口的 <code class=" language-php">RedisEventPusher</code> 实现类后,我们就可以在服务容器中像下面例子一样注册它:</p><pre class=" language-php"><code class=" language-php"><span class="token this">$this</span><span class="token operator">-</span><span class="token operator">></span><span class="token property">app</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">bind<span class="token punctuation">(</span></span>
<span class="token string">'App\Contracts\EventPusher'</span><span class="token punctuation">,</span>
<span class="token string">'App\Services\RedisEventPusher'</span>
<span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><p>这么做会告诉容器当一个类需要 <code class=" language-php">EventPusher</code> 接口的实例时, <code class=" language-php">RedisEventPusher</code> 的实例将会被容器注入。现在我们就可以在构造函数中,或者任何其他需要通过容器注入依赖的地方,使用 <code class=" language-php">EventPusher</code> 接口的类型提示:</p><pre class=" language-php"><code class=" language-php"><span class="token keyword">use</span> <span class="token package">App<span class="token punctuation">\</span>Contracts<span class="token punctuation">\</span>EventPusher</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">/**
* Create a new class instance.
*
* @param EventPusher $pusher
* @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>EventPusher <span class="token variable">$pusher</span><span class="token punctuation">)</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">pusher</span> <span class="token operator">=</span> <span class="token variable">$pusher</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span></code></pre><p><a name="contextual-binding"></a></p><h3>情境绑定</h3><p>有时候,你可能有两个类使用到相同的接口,但你希望每个类都能注入不同的实现。例如,两个控制器可能需要依赖不同的 <code class=" language-php">Illuminate\<span class="token package">Contracts<span class="token punctuation">\</span>Filesystem<span class="token punctuation">\</span>Filesystem</span></code> <a href="/docs/5.4/contracts">契约</a> 的实现类。 Laravel 为此定义了一种简单、平滑的接口:</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>Storage</span><span class="token punctuation">;</span>
<span class="token keyword">use</span> <span class="token package">App<span class="token punctuation">\</span>Http<span class="token punctuation">\</span>Controllers<span class="token punctuation">\</span>PhotoController</span><span class="token punctuation">;</span>
<span class="token keyword">use</span> <span class="token package">App<span class="token punctuation">\</span>Http<span class="token punctuation">\</span>Controllers<span class="token punctuation">\</span>VideoController</span><span class="token punctuation">;</span>
<span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Contracts<span class="token punctuation">\</span>Filesystem<span class="token punctuation">\</span>Filesystem</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">app</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">when<span class="token punctuation">(</span></span><span class="token scope">PhotoController<span class="token punctuation">::</span></span><span class="token keyword">class</span><span class="token punctuation">)</span>
<span class="token operator">-</span><span class="token operator">></span><span class="token function">needs<span class="token punctuation">(</span></span><span class="token scope">Filesystem<span class="token punctuation">::</span></span><span class="token keyword">class</span><span class="token punctuation">)</span>
<span class="token operator">-</span><span class="token operator">></span><span class="token function">give<span class="token punctuation">(</span></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 scope">Storage<span class="token punctuation">::</span></span><span class="token function">disk<span class="token punctuation">(</span></span><span class="token string">'local'</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 this">$this</span><span class="token operator">-</span><span class="token operator">></span><span class="token property">app</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">when<span class="token punctuation">(</span></span><span class="token scope">VideoController<span class="token punctuation">::</span></span><span class="token keyword">class</span><span class="token punctuation">)</span>
<span class="token operator">-</span><span class="token operator">></span><span class="token function">needs<span class="token punctuation">(</span></span><span class="token scope">Filesystem<span class="token punctuation">::</span></span><span class="token keyword">class</span><span class="token punctuation">)</span>
<span class="token operator">-</span><span class="token operator">></span><span class="token function">give<span class="token punctuation">(</span></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 scope">Storage<span class="token punctuation">::</span></span><span class="token function">disk<span class="token punctuation">(</span></span><span class="token string">'s3'</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><a name="tagging"></a></p><h3>标记</h3><p>有时候,你可能需要解析某个「分类」下的所有绑定。例如,你正在构建一个报表的聚合器,它需要接受不同 <code class=" language-php">Report</code> 接口的实例。分别注册了 <code class=" language-php">Report</code> 实例后,你可以使用 <code class=" language-php">tag</code> 方法为他们赋予一个标签:</p><pre class=" language-php"><code class=" language-php"><span class="token this">$this</span><span class="token operator">-</span><span class="token operator">></span><span class="token property">app</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">bind<span class="token punctuation">(</span></span><span class="token string">'SpeedReport'</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 comment" spellcheck="true"> //
</span><span class="token punctuation">}</span><span class="token punctuation">)</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">app</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">bind<span class="token punctuation">(</span></span><span class="token string">'MemoryReport'</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 comment" spellcheck="true"> //
</span><span class="token punctuation">}</span><span class="token punctuation">)</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">app</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">tag<span class="token punctuation">(</span></span><span class="token punctuation">[</span><span class="token string">'SpeedReport'</span><span class="token punctuation">,</span> <span class="token string">'MemoryReport'</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token string">'reports'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><p>一旦服务被标记后,你可以通过 <code class=" language-php">tagged</code> 方法轻松地将它们全部解析:</p><pre class=" language-php"><code class=" language-php"><span class="token this">$this</span><span class="token operator">-</span><span class="token operator">></span><span class="token property">app</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">bind<span class="token punctuation">(</span></span><span class="token string">'ReportAggregator'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token variable">$app</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token keyword">return</span> <span class="token keyword">new</span> <span class="token class-name">ReportAggregator</span><span class="token punctuation">(</span><span class="token variable">$app</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">tagged<span class="token punctuation">(</span></span><span class="token string">'reports'</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><a name="resolving"></a></p><h2><a href="#resolving">解析</a></h2><p><a name="the-make-method"></a></p><h4><code class=" language-php">make</code> 方法</h4><p>你可以在服务容器外使用 <code class=" language-php">make</code> 方法来获得一个实例化的类。它接受你希望解析的类或是接口名称作为参数:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$api</span> <span class="token operator">=</span> <span class="token this">$this</span><span class="token operator">-</span><span class="token operator">></span><span class="token property">app</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">make<span class="token punctuation">(</span></span><span class="token string">'HelpSpot\API'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><p>如果你的代码不能直接使用 <code class=" language-php"><span class="token variable">$app</span></code> 变量,你可以使用全局的 <code class=" language-php">resolve</code> 助手:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$api</span> <span class="token operator">=</span> <span class="token function">resolve<span class="token punctuation">(</span></span><span class="token string">'HelpSpot\API'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><p><a name="automatic-injection"></a></p><h4>自动注入</h4><p>另外,并且也是重要的,你可以在类的构造函数中对依赖使用「类型提示」,依赖的类将会被容器自动进行解析,包括在 <a href="/docs/5.4/controllers">控制器</a> , <a href="/docs/5.4/events">事件监听器</a> , <a href="/docs/5.4/queues">队列任务</a> , <a href="/docs/5.4/middleware">中间件</a> 等地方。 事实上,这也是大部分类被容器解析的方式。</p><p>例如,你可以在控制器的构造函数中对应用程序定义的 <code class=" language-php">Repository</code> 使用类型提示。这样 <code class=" language-php">Repository</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>Controllers</span><span class="token punctuation">;</span>
<span class="token keyword">use</span> <span class="token package">App<span class="token punctuation">\</span>Users<span class="token punctuation">\</span>Repository</span> <span class="token keyword">as</span> UserRepository<span class="token punctuation">;</span>
<span class="token keyword">class</span> <span class="token class-name">UserController</span> <span class="token keyword">extends</span> <span class="token class-name">Controller</span>
<span class="token punctuation">{</span>
<span class="token comment" spellcheck="true">/**
* user repository 实例。
*/</span>
<span class="token keyword">protected</span> <span class="token variable">$users</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">/**
* 控制器构造方法。
*
* @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 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">/**
* 显示指定 ID 的用户信息。
*
* @param int $id
* @return Response
*/</span>
<span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function">show<span class="token punctuation">(</span></span><span class="token variable">$id</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="container-events"></a></p><h2><a href="#container-events">容器事件</a></h2><p>每当服务容器解析一个对象时就会触发一个事件。你可以使用 <code class=" language-php">resolving</code> 方法监听这个事件:</p><pre class=" language-php"><code class=" language-php"><span class="token this">$this</span><span class="token operator">-</span><span class="token operator">></span><span class="token property">app</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">resolving<span class="token punctuation">(</span></span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token variable">$object</span><span class="token punctuation">,</span> <span class="token variable">$app</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 this">$this</span><span class="token operator">-</span><span class="token operator">></span><span class="token property">app</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">resolving<span class="token punctuation">(</span></span><span class="token scope">HelpSpot<span class="token punctuation">\</span>API<span class="token punctuation">::</span></span><span class="token keyword">class</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token variable">$api</span><span class="token punctuation">,</span> <span class="token variable">$app</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token comment" spellcheck="true"> // 解析「HelpSpot\API」类型的对象时调用...
</span><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><p>如你所见,被解析的对象会被传递至回调中,让你在对象被传递到消费者前可以设置任何额外属性到对象上。</p></article>
- 入门指南
- 安装
- 配置信息
- 文件夹结构
- 请求周期
- 开发环境部署
- Valet
- Homestead
- 核心概念
- 服务提供者
- Facades
- Contracts
- 服务容器
- HTTP 层
- 路由
- 中间件
- CSRF 保护
- 控制器
- 请求
- 响应
- 视图
- Session
- 表单验证
- 前端
- Blade 模板
- 本地化
- 前端指南
- 编辑资源 Mix
- 安全
- API 认证
- 用户认证
- 用户授权
- 加密解密
- 哈希
- 重置密码
- 数据库
- 快速入门
- 查询构造器
- 分页
- 数据库迁移
- Redis
- 数据填充
- Eloquent ORM
- Eloquent ORM快速入门
- 模型关联
- Eloquent 集合
- 修改器
- 序列化
- 综合话题
- Artisan 命令行
- 广播系统
- 缓存系统
- 集合
- 错误与日志
- 事件系统
- 文件存储
- 辅助函数
- 邮件发送
- 消息通知
- 扩展包开发
- 队列
- 任务调度