<article><h1>Laravel 的消息通知系统</h1><ul><li><a href="#introduction">简介</a></li><li><a href="#creating-notifications">创建通知</a></li><li><a href="#sending-notifications">发送通知</a><ul><li><a href="#using-the-notifiable-trait">使用 Notifiable Trait</a></li><li><a href="#using-the-notification-facade">使用 Notification Facade</a></li><li><a href="#specifying-delivery-channels">指定发送频道</a></li><li><a href="#queueing-notifications">队列化通知</a></li></ul></li><li><a href="#mail-notifications">邮件通知</a><ul><li><a href="#formatting-mail-messages">格式化邮件消息</a></li><li><a href="#customizing-the-recipient">自定义接受者</a></li><li><a href="#customizing-the-subject">自定义主题</a></li><li><a href="#customizing-the-templates">自定义模板</a></li></ul></li><li><a href="#markdown-mail-notifications">Markdown 邮件通知</a><ul><li><a href="#generating-the-message">生成消息</a></li><li><a href="#writing-the-message">写消息</a></li><li><a href="#customizing-the-components">自定义组件</a></li></ul></li><li><a href="#database-notifications">数据库通知</a><ul><li><a href="#database-prerequisites">先决条件</a></li><li><a href="#formatting-database-notifications">格式化数据库通知</a></li><li><a href="#accessing-the-notifications">访问通知</a></li><li><a href="#marking-notifications-as-read">标为已读</a></li></ul></li><li><a href="#broadcast-notifications">广播通知</a><ul><li><a href="#broadcast-prerequisites">先决条件</a></li><li><a href="#formatting-broadcast-notifications">格式化广播通知</a></li><li><a href="#listening-for-notifications">监听通知</a></li></ul></li><li><a href="#sms-notifications">短信通知</a><ul><li><a href="#sms-prerequisites">先决条件</a></li><li><a href="#formatting-sms-notifications">格式化短信通知</a></li><li><a href="#customizing-the-from-number">自定义 <code class=" language-php">From</code> 号码</a></li><li><a href="#routing-sms-notifications">路由短信通知</a></li></ul></li><li><a href="#slack-notifications">Slack 通知</a><ul><li><a href="#slack-prerequisites">先决条件</a></li><li><a href="#formatting-slack-notifications">格式化 Slack 通知</a></li><li><a href="#slack-attachments">Slack Attachments</a></li><li><a href="#routing-slack-notifications">路由 Slack 通知</a></li></ul></li><li><a href="#notification-events">通知事件</a></li><li><a href="#custom-channels">自定义发送频道</a></li></ul><p><a name="introduction"></a></p><h2><a href="#introduction">简介</a></h2><p>除了 <a href="/docs/5.4/mail">发送邮件</a>,Laravel 还支持通过多种频道发送通知,包括邮件、短信(通过 <a href="https://www.nexmo.com/">Nexmo</a>)以及 <a href="https://slack.com">Slack</a> 。通知还能存到数据库,这样就能在网页界面上显示了。</p><p>通常情况下,通知应该是简短、有信息量的消息来通知用户你的应用发生了什么。举例来说,如果你在编写一个在线交易应用,你应该会通过邮件和短信频道来给用户发送一条 「账单已付」 的通知。</p><p><a name="creating-notifications"></a></p><h2><a href="#creating-notifications">创建通知</a></h2><p>Laravel 中一条通知就是一个类(通常存在 <code class=" language-php">app<span class="token operator">/</span>Notifications</code> 文件夹里)。看不到的话不要担心,运行一下 <code class=" language-php">make<span class="token punctuation">:</span>notification</code> 命令就能创建了:</p><pre class=" language-php"><code class=" language-php">php artisan make<span class="token punctuation">:</span>notification InvoicePaid</code></pre><p>这个命令会在 <code class=" language-php">app<span class="token operator">/</span>Notifications</code> 目录下生成一个新的通知类。这个类包含 <code class=" language-php">via</code> 方法和几个消息构建方法(比如 <code class=" language-php">toMail</code> 或 <code class=" language-php">toDatabase</code>),它们会针对指定的渠道把通知转换过为对应的消息。</p><p><a name="sending-notifications"></a></p><h2><a href="#sending-notifications">发送通知</a></h2><p><a name="using-the-notifiable-trait"></a></p><h3>使用 Notifiable Trait</h3><p>通知可以通过两种方法发送: <code class=" language-php">Notifiable</code> trait 的 <code class=" language-php">notify</code> 方法或 <code class=" language-php">Notification</code> <a href="/docs/5.4/facades">facade</a> 。首先,让我们探索使用 trait :</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><span class="token punctuation">;</span>
<span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Notifications<span class="token punctuation">\</span>Notifiable</span><span class="token punctuation">;</span>
<span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Foundation<span class="token punctuation">\</span>Auth<span class="token punctuation">\</span>User</span> <span class="token keyword">as</span> Authenticatable<span class="token punctuation">;</span>
<span class="token keyword">class</span> <span class="token class-name">User</span> <span class="token keyword">extends</span> <span class="token class-name">Authenticatable</span>
<span class="token punctuation">{</span>
<span class="token keyword">use</span> <span class="token package">Notifiable</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span></code></pre><p>默认的 <code class=" language-php">App\<span class="token package">User</span></code> 模型中使用了这个 trait,它包含着一个可以用来发通知的方法:<code class=" language-php">notify</code> 。 <code class=" language-php">notify</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>Notifications<span class="token punctuation">\</span>InvoicePaid</span><span class="token punctuation">;</span>
<span class="token variable">$user</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">notify<span class="token punctuation">(</span></span><span class="token keyword">new</span> <span class="token class-name">InvoicePaid</span><span class="token punctuation">(</span><span class="token variable">$invoice</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><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> 记住,你可以在任意模型中使用 <code class=" language-php">Illuminate\<span class="token package">Notifications<span class="token punctuation">\</span>Notifiable</span></code> trait,而不仅仅是在 <code class=" language-php">User</code> 模型中。</p></blockquote><p><a name="using-the-notification-facade"></a></p><h3>使用 Notification Facade</h3><p>另外,你可以通过 <code class=" language-php">Notification</code> <a href="/docs/5.4/facades">facade</a> 来发送通知。它主要用在当你给多个可接收通知的实体发送通知的时候,比如给用户集合发通知。要用 facade 发送通知的话,要把可接收通知的实体和通知的实例传递给 <code class=" language-php">send</code> 方法:</p><pre class=" language-php"><code class=" language-php"><span class="token scope">Notification<span class="token punctuation">::</span></span><span class="token function">send<span class="token punctuation">(</span></span><span class="token variable">$users</span><span class="token punctuation">,</span> <span class="token keyword">new</span> <span class="token class-name">InvoicePaid</span><span class="token punctuation">(</span><span class="token variable">$invoice</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><p><a name="specifying-delivery-channels"></a></p><h3>指定发送频道</h3><p>每个通知类都有个 <code class=" language-php">via</code> 方法,它决定了通知在哪个频道上发送。开箱即用的通知频道有 <code class=" language-php">mail</code>, <code class=" language-php">database</code>, <code class=" language-php">broadcast</code>, <code class=" language-php">nexmo</code>, 和 <code class=" language-php">slack</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> 如果你想用其他的频道比如 Telegram 或者 Pusher ,可以去看下社区驱动的 <a href="http://laravel-notification-channels.com">Laravel 通知频道网站</a> 。</p></blockquote><p><code class=" language-php">via</code> 方法受到一个 <code class=" language-php"><span class="token variable">$notifiable</span></code> 实例,它是接收通知的类实例。你可以用 <code class=" language-php"><span class="token variable">$notifiable</span></code> 来决定通知用哪个频道来发送:</p><pre class=" language-php"><code class=" language-php"><span class="token comment" spellcheck="true">/**
* 获取通知发送频道
*
* @param mixed $notifiable
* @return array
*/</span>
<span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function">via<span class="token punctuation">(</span></span><span class="token variable">$notifiable</span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
<span class="token keyword">return</span> <span class="token variable">$notifiable</span><span class="token operator">-</span><span class="token operator">></span><span class="token property">prefers_sms</span> <span class="token operator">?</span> <span class="token punctuation">[</span><span class="token string">'nexmo'</span><span class="token punctuation">]</span> <span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token string">'mail'</span><span class="token punctuation">,</span> <span class="token string">'database'</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span></code></pre><p><a name="queueing-notifications"></a></p><h3>队列化通知</h3><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/queues">运行队列处理器</a>。</p></blockquote><p>发送通知可能会花很长时间,尤其是发送频道需要调用外部 API 的时候。要加速应用响应的话,可以通过添加 <code class=" language-php">ShouldQueue</code> 接口和 <code class=" language-php">Queueable</code> trait 把通知加入队列。它们两个在使用 <code class=" language-php">make<span class="token punctuation">:</span>notification</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>Notifications</span><span class="token punctuation">;</span>
<span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Bus<span class="token punctuation">\</span>Queueable</span><span class="token punctuation">;</span>
<span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Notifications<span class="token punctuation">\</span>Notification</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>Queue<span class="token punctuation">\</span>ShouldQueue</span><span class="token punctuation">;</span>
<span class="token keyword">class</span> <span class="token class-name">InvoicePaid</span> <span class="token keyword">extends</span> <span class="token class-name">Notification</span> <span class="token keyword">implements</span> <span class="token class-name">ShouldQueue</span>
<span class="token punctuation">{</span>
<span class="token keyword">use</span> <span class="token package">Queueable</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true"> // ...
</span><span class="token punctuation">}</span></code></pre><p>一旦加入 <code class=" language-php">ShouldQueue</code> 接口,你就能像平常那样发送通知了。Laravel 会检测 <code class=" language-php">ShouldQueue</code> 接口并自动将通知的发送放入队列中。</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$user</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">notify<span class="token punctuation">(</span></span><span class="token keyword">new</span> <span class="token class-name">InvoicePaid</span><span class="token punctuation">(</span><span class="token variable">$invoice</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><p>如果你想延迟发送,你可以通过 <code class=" language-php">delay</code> 方法来链式操作你的通知实例:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$when</span> <span class="token operator">=</span> <span class="token scope">Carbon<span class="token punctuation">::</span></span><span class="token function">now<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">addMinutes<span class="token punctuation">(</span></span><span class="token number">10</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 operator">></span><span class="token function">notify<span class="token punctuation">(</span></span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">InvoicePaid</span><span class="token punctuation">(</span><span class="token variable">$invoice</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">delay<span class="token punctuation">(</span></span><span class="token variable">$when</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><p><a name="mail-notifications"></a></p><h2><a href="#mail-notifications">邮件通知</a></h2><p><a name="formatting-mail-messages"></a></p><h3>格式化邮件消息</h3><p>如果一条通知支持以邮件发送,你应该在通知类里定义一个 <code class=" language-php">toMail</code> 方法。这个方法将收到一个 <code class=" language-php"><span class="token variable">$notifiable</span></code> 实体并返回一个 <code class=" language-php">Illuminate\<span class="token package">Notifications<span class="token punctuation">\</span>Messages<span class="token punctuation">\</span>MailMessage</span></code> 实例。邮件消息可以包含多行文本也可以是引导链接。我们来看一个 <code class=" language-php">toMail</code> 方法的例子:</p><pre class=" language-php"><code class=" language-php"><span class="token comment" spellcheck="true">/**
* 获取通知的邮件展示方式
*
* @param mixed $notifiable
* @return \Illuminate\Notifications\Messages\MailMessage
*/</span>
<span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function">toMail<span class="token punctuation">(</span></span><span class="token variable">$notifiable</span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
<span class="token variable">$url</span> <span class="token operator">=</span> <span class="token function">url<span class="token punctuation">(</span></span><span class="token string">'/invoice/'</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">invoice</span><span class="token operator">-</span><span class="token operator">></span><span class="token property">id</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">return</span> <span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">MailMessage</span><span class="token punctuation">)</span>
<span class="token operator">-</span><span class="token operator">></span><span class="token function">line<span class="token punctuation">(</span></span><span class="token string">'One of your invoices has been paid!'</span><span class="token punctuation">)</span>
<span class="token operator">-</span><span class="token operator">></span><span class="token function">action<span class="token punctuation">(</span></span><span class="token string">'View Invoice'</span><span class="token punctuation">,</span> <span class="token variable">$url</span><span class="token punctuation">)</span>
<span class="token operator">-</span><span class="token operator">></span><span class="token function">line<span class="token punctuation">(</span></span><span class="token string">'Thank you for using our application!'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span></code></pre><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> 注意我们在方法中用了 <code class=" language-php"><span class="token this">$this</span><span class="token operator">-</span><span class="token operator">></span><span class="token property">invoice</span><span class="token operator">-</span><span class="token operator">></span><span class="token property">id</span></code> ,其实你可以传递应用所需要的任何数据来传递给通知的构造器。</p></blockquote><p>在这个例子中,我们注册了一行文本,引导链接 ,然后又是一行文本。 <code class=" language-php">MailMessage</code> 提供的这些方法简化了对小的事务性的邮件进行格式化操作。邮件频道将会把这些消息组件转换成漂亮的响应式的 HTML 邮件模板并附上文本。下面是个 <code class=" language-php">mail</code> 频道生成的邮件示例:</p><img src="https://laravel.com/assets/img/notification-example.png" width="551" height="596"><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> 发送邮件通知前,请在 <code class=" language-php">config<span class="token operator">/</span>app<span class="token punctuation">.</span>php</code> 中设置 <code class=" language-php">name</code> 值。 这将会在邮件通知消息的头部和尾部中被使用。</p></blockquote><h4>其他通知格式选项</h4><p>你可以使用 <code class=" language-php">view</code> 方法来指定一个应用于渲染通知电子邮件的自定义模板,而不是在通知类中定义文本的「模板」:</p><pre class=" language-php"><code class=" language-php"><span class="token comment" spellcheck="true">/**
* 获取通知的邮件展示方式
*
* @param mixed $notifiable
* @return \Illuminate\Notifications\Messages\MailMessage
*/</span>
<span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function">toMail<span class="token punctuation">(</span></span><span class="token variable">$notifiable</span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
<span class="token keyword">return</span> <span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">MailMessage</span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">view<span class="token punctuation">(</span></span>
<span class="token string">'emails.name'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string">'invoice'</span> <span class="token operator">=</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">invoice</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">toMail</code> 方法中返回一个 <a href="/docs/5.4/mail">mailable 对象</a> :</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>Mail<span class="token punctuation">\</span>InvoicePaid</span> <span class="token keyword">as</span> Mailable<span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">/**
* 获取通知的邮件展示方式
*
* @param mixed $notifiable
* @return Mailable
*/</span>
<span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function">toMail<span class="token punctuation">(</span></span><span class="token variable">$notifiable</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">Mailable</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">invoice</span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">to<span class="token punctuation">(</span></span><span class="token this">$this</span><span class="token operator">-</span><span class="token operator">></span><span class="token property">user</span><span class="token operator">-</span><span class="token operator">></span><span class="token property">email</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span></code></pre><p><a name="error-messages"></a></p><h4>错误消息</h4><p>有些通知是给用户提示错误,比如账单支付失败的提示。你可以通过调用 <code class=" language-php">error</code> 方法来指定这条邮件消息被当做一个错误提示。当邮件消息使用了 <code class=" language-php">error</code> 方法后,引导链接按钮会变成红色而非蓝色:</p><pre class=" language-php"><code class=" language-php"><span class="token comment" spellcheck="true">/**
* 获取通知的邮件展示方式
*
* @param mixed $notifiable
* @return \Illuminate\Notifications\Messages\MailMessage
*/</span>
<span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function">toMail<span class="token punctuation">(</span></span><span class="token variable">$notifiable</span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
<span class="token keyword">return</span> <span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">MailMessage</span><span class="token punctuation">)</span>
<span class="token operator">-</span><span class="token operator">></span><span class="token function">error<span class="token punctuation">(</span></span><span class="token punctuation">)</span>
<span class="token operator">-</span><span class="token operator">></span><span class="token function">subject<span class="token punctuation">(</span></span><span class="token string">'Notification Subject'</span><span class="token punctuation">)</span>
<span class="token operator">-</span><span class="token operator">></span><span class="token function">line<span class="token punctuation">(</span></span><span class="token string">'...'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span></code></pre><p><a name="customizing-the-recipient"></a></p><h3>自定义接收者</h3><p>当通过 <code class=" language-php">mail</code> 频道来发送通知的时候,通知系统将会自动寻找你的 notifiable 实体中的 <code class=" language-php">email</code> 属性。你可以通过在实体中定义 <code class=" language-php">routeNotificationForMail</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><span class="token punctuation">;</span>
<span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Notifications<span class="token punctuation">\</span>Notifiable</span><span class="token punctuation">;</span>
<span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Foundation<span class="token punctuation">\</span>Auth<span class="token punctuation">\</span>User</span> <span class="token keyword">as</span> Authenticatable<span class="token punctuation">;</span>
<span class="token keyword">class</span> <span class="token class-name">User</span> <span class="token keyword">extends</span> <span class="token class-name">Authenticatable</span>
<span class="token punctuation">{</span>
<span class="token keyword">use</span> <span class="token package">Notifiable</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">/**
* 邮件频道的路由
*
* @return string
*/</span>
<span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function">routeNotificationForMail<span class="token punctuation">(</span></span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
<span class="token keyword">return</span> <span class="token this">$this</span><span class="token operator">-</span><span class="token operator">></span><span class="token property">email_address</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token punctuation">}</span></code></pre><p><a name="customizing-the-subject"></a></p><h3>自定义主题</h3><p>默认情况下,邮件主题是格式化成了标题格式的通知类的类名。所以如果你对通知类名为 <code class=" language-php">InvoicePaid</code> ,邮件主题将会是 <code class=" language-php">Invoice Paid</code> 。如果你想显式指定消息的主题,你可以在构建消息时调用 <code class=" language-php">subject</code> 方法:</p><pre class=" language-php"><code class=" language-php"><span class="token comment" spellcheck="true">/**
* 获取通知的邮件展示方式
*
* @param mixed $notifiable
* @return \Illuminate\Notifications\Messages\MailMessage
*/</span>
<span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function">toMail<span class="token punctuation">(</span></span><span class="token variable">$notifiable</span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
<span class="token keyword">return</span> <span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">MailMessage</span><span class="token punctuation">)</span>
<span class="token operator">-</span><span class="token operator">></span><span class="token function">subject<span class="token punctuation">(</span></span><span class="token string">'Notification Subject'</span><span class="token punctuation">)</span>
<span class="token operator">-</span><span class="token operator">></span><span class="token function">line<span class="token punctuation">(</span></span><span class="token string">'...'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span></code></pre><p><a name="customizing-the-templates"></a></p><h3>自定义模板</h3><p>你可以通过发布通知包的资源来修改 HTML 模板和纯文本模板。运行这个命令后,邮件通知模板就被放在了 <code class=" language-php">resources<span class="token operator">/</span>views<span class="token operator">/</span>vendor<span class="token operator">/</span>notifications</code> 文件夹下:</p><pre class=" language-php"><code class=" language-php">php artisan vendor<span class="token punctuation">:</span>publish <span class="token operator">--</span>tag<span class="token operator">=</span>laravel<span class="token operator">-</span>notifications</code></pre><p><a name="markdown-mail-notifications"></a></p><h2><a href="#markdown-mail-notifications">Markdown 邮件通知</a></h2><p>Markdown 邮件通知可让您利用邮件通知的预先构建的模板,同时给予您更多自由地撰写更长的自定义邮件。 由于消息是用 Markdown 编写的, Laravel 能够为消息呈现漂亮,响应的 HTML 模板,同时还自动生成纯文本对应。</p><p><a name="generating-the-message"></a></p><h3>生成消息</h3><p>要使用相应的 Markdown 模板生成通知,您可以使用 <code class=" language-php">make:notification</code> Artisan命令的 <code class=" language-php"><span class="token operator">--</span>markdown</code> 选项:</p><pre class=" language-php"><code class=" language-php">php artisan make<span class="token punctuation">:</span>notification InvoicePaid <span class="token operator">--</span>markdown<span class="token operator">=</span>mail<span class="token punctuation">.</span>invoice<span class="token punctuation">.</span>paid</code></pre><p>与所有其他邮件通知一样,使用 Markdown 模板的通知应在其通知类上定义一个 <code class=" language-php">toMail</code> 方法。 但是,不使用 <code class=" language-php">line</code> 和 <code class=" language-php">action</code> 方法来构造通知,而是使用 <code class=" language-php">markdown</code> 方法来指定应该使用的 Markdown 模板的名称:</p><pre class=" language-php"><code class=" language-php"><span class="token comment" spellcheck="true">/**
* 获取通知的邮件展示方式
*
* @param mixed $notifiable
* @return \Illuminate\Notifications\Messages\MailMessage
*/</span>
<span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function">toMail<span class="token punctuation">(</span></span><span class="token variable">$notifiable</span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
<span class="token variable">$url</span> <span class="token operator">=</span> <span class="token function">url<span class="token punctuation">(</span></span><span class="token string">'/invoice/'</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">invoice</span><span class="token operator">-</span><span class="token operator">></span><span class="token property">id</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">return</span> <span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">MailMessage</span><span class="token punctuation">)</span>
<span class="token operator">-</span><span class="token operator">></span><span class="token function">subject<span class="token punctuation">(</span></span><span class="token string">'Invoice Paid'</span><span class="token punctuation">)</span>
<span class="token operator">-</span><span class="token operator">></span><span class="token function">markdown<span class="token punctuation">(</span></span><span class="token string">'mail.invoice.paid'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string">'url'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token variable">$url</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="writing-the-message"></a></p><h3>写消息</h3><p>Markdown 邮件通知使用 Blade 组件和Markdown语法的组合,允许您轻松构建通知,同时利用Laravel的预制通知组件:</p><pre class=" language-php"><code class=" language-php">@<span class="token function">component<span class="token punctuation">(</span></span><span class="token string">'mail::message'</span><span class="token punctuation">)</span><span class="token comment" spellcheck="true">
# Invoice Paid
</span>
Your invoice has been paid<span class="token operator">!</span>
@<span class="token function">component<span class="token punctuation">(</span></span><span class="token string">'mail::button'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string">'url'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token variable">$url</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
View Invoice
@endcomponent
Thanks<span class="token punctuation">,</span><span class="token markup"><span class="token tag"><span class="token tag"><span class="token punctuation"><</span>br</span><span class="token punctuation">></span></span></span>
<span class="token punctuation">{</span><span class="token punctuation">{</span> <span class="token function">config<span class="token punctuation">(</span></span><span class="token string">'app.name'</span><span class="token punctuation">)</span> <span class="token punctuation">}</span><span class="token punctuation">}</span>
@endcomponent</code></pre><h4>按钮组件</h4><p>按钮组件渲染了一个居中的链接按钮。组件有两个参数, <code class=" language-php">url</code> 和一个可选的参数 <code class=" language-php">color</code> 。支持的颜色有 <code class=" language-php">blue</code> , <code class=" language-php">green</code> , 和 <code class=" language-php">red</code> 。你可以根据你的需要向通知中添加任意数量的按钮组件:</p><pre class=" language-php"><code class=" language-php">@<span class="token function">component<span class="token punctuation">(</span></span><span class="token string">'mail::button'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string">'url'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token variable">$url</span><span class="token punctuation">,</span> <span class="token string">'color'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token string">'green'</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
View Invoice
@endcomponent</code></pre><h4>面板组件</h4><p>面板部件在面板中呈现给定文本块,该面板具有与通知的其余部分稍微不同的背景颜色。 这将会允许你将给定的文本块引起注意:</p><pre class=" language-php"><code class=" language-php">@<span class="token function">component<span class="token punctuation">(</span></span><span class="token string">'mail::panel'</span><span class="token punctuation">)</span>
This is the panel content<span class="token punctuation">.</span>
@endcomponent</code></pre><h4>表格组件</h4><p>表格组件允许你将一个 Markdown 表格转化成一个 HTML 表格。表格组件接受 Markdown 表格作为内容。支持使用默认的 Markdown 表格对齐语法使表列对齐:</p><pre class=" language-php"><code class=" language-php">@<span class="token function">component<span class="token punctuation">(</span></span><span class="token string">'mail::table'</span><span class="token punctuation">)</span>
<span class="token operator">|</span> Laravel <span class="token operator">|</span> Table <span class="token operator">|</span> Example <span class="token operator">|</span>
<span class="token operator">|</span> <span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">-</span> <span class="token operator">|</span><span class="token punctuation">:</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">-</span><span class="token punctuation">:</span><span class="token operator">|</span> <span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token punctuation">:</span><span class="token operator">|</span>
<span class="token operator">|</span> Col <span class="token number">2</span> is <span class="token operator">|</span> Centered <span class="token operator">|</span> <span class="token variable">$10</span> <span class="token operator">|</span>
<span class="token operator">|</span> Col <span class="token number">3</span> is <span class="token operator">|</span> Right<span class="token operator">-</span>Aligned <span class="token operator">|</span> <span class="token variable">$20</span> <span class="token operator">|</span>
@endcomponent</code></pre><p><a name="customizing-the-components"></a></p><h3>自定义组件</h3><p>您可以将所有 Markdown 通知组件导出到您自己的应用程序中进行自定义。 要导出组件,请使用 <code class=" language-php">vendor:publish</code> Artisan命令来发布 <code class=" language-php">laravel<span class="token operator">-</span>mail</code> 标签:</p><pre class=" language-php"><code class=" language-php">php artisan vendor<span class="token punctuation">:</span>publish <span class="token operator">--</span>tag<span class="token operator">=</span>laravel<span class="token operator">-</span>mail</code></pre><p>此命令将 Markdown 邮件组件发布到 <code class=" language-php">resources<span class="token operator">/</span>views<span class="token operator">/</span>vendor<span class="token operator">/</span>mail</code> 目录。 <code class=" language-php">mail</code> 目录将包含一个 <code class=" language-php">html</code> 和一个 <code class=" language-php">markdown</code> 目录,每个目录包含它们各自可用组件的展示方式。 您可以随意自定义这些组件。</p><h4>自定义CSS</h4><p>导出组件后, <code class=" language-php">resources<span class="token operator">/</span>views<span class="token operator">/</span>vendor<span class="token operator">/</span>mail<span class="token operator">/</span>html<span class="token operator">/</span>themes</code> 目录将包含一个 <code class=" language-php"><span class="token keyword">default</span><span class="token punctuation">.</span>css</code> 文件。 您可以在此文件中自定义 CSS ,并且您的样式将自动内嵌在 Markdown 通知的HTML展示中。</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> 如果您想为 Markdown 组件构建一个全新的主题,只需在 <code class=" language-php">html<span class="token operator">/</span>themes</code> 目录中写一个新的 CSS 文件,并更改 <code class=" language-php">mail</code> 配置文件中的 <code class=" language-php">theme</code> 选项。</p></blockquote><p><a name="database-notifications"></a></p><h2><a href="#database-notifications">数据库通知</a></h2><p><a name="database-prerequisites"></a></p><h3>先决条件</h3><p><code class=" language-php">database</code> 通知频道在一张数据表里存储通知信息。这张表包含了比如通知类型、JSON 格式数据等描述通知的信息。</p><p>你可以查询这张表的内容在应用界面上展示通知。但是在这之前,你需要先创建一个数据表来保存通知。你可以用 <code class=" language-php">notifications<span class="token punctuation">:</span>table</code> 命令来生成迁移表:</p><pre class=" language-php"><code class=" language-php">php artisan notifications<span class="token punctuation">:</span>table
php artisan migrate</code></pre><p><a name="formatting-database-notifications"></a></p><h3>格式化数据库通知</h3><p>如果通知支持被存储到数据表中,你应该在通知类中定义一个 <code class=" language-php">toDatabase</code> 或 <code class=" language-php">toArray</code> 方法。这个方法接收 <code class=" language-php"><span class="token variable">$notifiable</span></code> 实体参数并返回一个普通的 PHP 数组。这个返回的数组将被转成 JSON 格式并存储到通知数据表的 <code class=" language-php">data</code> 列。我们来看一个 <code class=" language-php">toArray</code> 的例子:</p><pre class=" language-php"><code class=" language-php"><span class="token comment" spellcheck="true">/**
* 获取通知的数组展示方式
*
* @param mixed $notifiable
* @return array
*/</span>
<span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function">toArray<span class="token punctuation">(</span></span><span class="token variable">$notifiable</span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
<span class="token keyword">return</span> <span class="token punctuation">[</span>
<span class="token string">'invoice_id'</span> <span class="token operator">=</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">invoice</span><span class="token operator">-</span><span class="token operator">></span><span class="token property">id</span><span class="token punctuation">,</span>
<span class="token string">'amount'</span> <span class="token operator">=</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">invoice</span><span class="token operator">-</span><span class="token operator">></span><span class="token property">amount</span><span class="token punctuation">,</span>
<span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span></code></pre><h4><code class=" language-php">toDatabase</code> Vs <code class=" language-php">toArray</code></h4><p><code class=" language-php">toArray</code> 方法在 <code class=" language-php">broadcast</code> 频道也用到了,它用来决定广播给 JavaScript 客户端的数据。如果你想在 <code class=" language-php">database</code> 和 <code class=" language-php">broadcast</code> 频道中采用两种不同的数组展示方式,你应该定义 <code class=" language-php">toDatabase</code> 方法而非 <code class=" language-php">toArray</code> 方法。</p><p><a name="accessing-the-notifications"></a></p><h3>访问通知</h3><p>一旦通知被存到数据库中,你需要一种方便的方式来从通知实体中访问它们。 <code class=" language-php">Illuminate\<span class="token package">Notifications<span class="token punctuation">\</span>Notifiable</span></code> trait 包含一个可以返回这个实体所有通知的 <code class=" language-php">notifications</code> Eloquent 关联。要获取这些通知,你可以像用其他 Eloquent 关联一样来使用这个方法。默认情况下,通知将会以 <code class=" language-php">created_at</code> 时间戳来排序:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$user</span> <span class="token operator">=</span> <span class="token scope">App<span class="token punctuation">\</span>User<span class="token punctuation">::</span></span><span class="token function">find<span class="token punctuation">(</span></span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">foreach</span> <span class="token punctuation">(</span><span class="token variable">$user</span><span class="token operator">-</span><span class="token operator">></span><span class="token property">notifications</span> <span class="token keyword">as</span> <span class="token variable">$notification</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token keyword">echo</span> <span class="token variable">$notification</span><span class="token operator">-</span><span class="token operator">></span><span class="token property">type</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span></code></pre><p>如果你只想检索未读通知,你可以使用 <code class=" language-php">unreadNotifications</code> 关联。检索出来的通知也是以 <code class=" language-php">created_at</code> 时间戳来排序的:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$user</span> <span class="token operator">=</span> <span class="token scope">App<span class="token punctuation">\</span>User<span class="token punctuation">::</span></span><span class="token function">find<span class="token punctuation">(</span></span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">foreach</span> <span class="token punctuation">(</span><span class="token variable">$user</span><span class="token operator">-</span><span class="token operator">></span><span class="token property">unreadNotifications</span> <span class="token keyword">as</span> <span class="token variable">$notification</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token keyword">echo</span> <span class="token variable">$notification</span><span class="token operator">-</span><span class="token operator">></span><span class="token property">type</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span></code></pre><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> 要从 JavaScript 客户端来访问通知的话,你应该定义一个通知控制器来给可通知的实体返回通知,比如给当前用户返回通知。然后你就可以在 JavaScript 客户端来发起对应 URI 的 HTTP 请求了。</p></blockquote><p><a name="marking-notifications-as-read"></a></p><h3>标为已读</h3><p>通常情况下,当用户查看了通知时,你就希望把通知标为已读。<code class=" language-php">Illuminate\<span class="token package">Notifications<span class="token punctuation">\</span>Notifiable</span></code> trait 提供了一个 <code class=" language-php">markAsRead</code> 方法,它能在对应的数据库记录里更新 <code class=" language-php">read_at</code> 列:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$user</span> <span class="token operator">=</span> <span class="token scope">App<span class="token punctuation">\</span>User<span class="token punctuation">::</span></span><span class="token function">find<span class="token punctuation">(</span></span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">foreach</span> <span class="token punctuation">(</span><span class="token variable">$user</span><span class="token operator">-</span><span class="token operator">></span><span class="token property">unreadNotifications</span> <span class="token keyword">as</span> <span class="token variable">$notification</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token variable">$notification</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">markAsRead<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span></code></pre><p>你可以使用 <code class=" language-php">markAsRead</code> 方法直接操作一个通知集合,而不是一条条遍历每个通知:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$user</span><span class="token operator">-</span><span class="token operator">></span><span class="token property">unreadNotifications</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">markAsRead<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><p>你可以用批量更新的方式来把所有通知标为已读,而不用在数据库里检索:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$user</span> <span class="token operator">=</span> <span class="token scope">App<span class="token punctuation">\</span>User<span class="token punctuation">::</span></span><span class="token function">find<span class="token punctuation">(</span></span><span class="token number">1</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 operator">></span><span class="token function">unreadNotifications<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">update<span class="token punctuation">(</span></span><span class="token punctuation">[</span><span class="token string">'read_at'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token scope">Carbon<span class="token punctuation">::</span></span><span class="token function">now<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></code></pre><p>当然,你可以通过 <code class=" language-php">delete</code> 通知来把它们从数据库删除:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$user</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">notifications<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">delete<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><p><a name="broadcast-notifications"></a></p><h2><a href="#broadcast-notifications">广播通知</a></h2><p><a name="broadcast-prerequisites"></a></p><h3>先决条件</h3><p>在广播通知前,你应该配置并熟悉 Laravel <a href="/docs/5.4/broadcasting">事件广播</a> 服务。事件广播提供了一种 JavaScript 客户端响应服务端 Laravel 事件的机制。</p><p><a name="formatting-broadcast-notifications"></a></p><h3>格式化广播通知</h3><p><code class=" language-php">broadcast</code> 频道使用 Laravel <a href="/docs/5.4/broadcasting">事件广播</a> 服务来广播通知,它使得 JavaScript 客户端可以实时捕捉通知。如果一条通知支持广播,你应该在通知类里定义一个 <code class=" language-php">toBroadcast</code> 或 <code class=" language-php">toArray</code> 方法。这个方法将收到一个 <code class=" language-php"><span class="token variable">$notifiable</span></code> 实体并返回一个普通的 PHP 数组。返回的数组会被编码成 JSON 格式并广播给你的 JavaScript 客户端。我们来看个 <code class=" language-php">toArray</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>Notifications<span class="token punctuation">\</span>Messages<span class="token punctuation">\</span>BroadcastMessage</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">/**
* 获取通知的数组展示方式
*
* @param mixed $notifiable
* @return BroadcastMessage
*/</span>
<span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function">toBroadcast<span class="token punctuation">(</span></span><span class="token variable">$notifiable</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">BroadcastMessage</span><span class="token punctuation">(</span><span class="token punctuation">[</span>
<span class="token string">'invoice_id'</span> <span class="token operator">=</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">invoice</span><span class="token operator">-</span><span class="token operator">></span><span class="token property">id</span><span class="token punctuation">,</span>
<span class="token string">'amount'</span> <span class="token operator">=</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">invoice</span><span class="token operator">-</span><span class="token operator">></span><span class="token property">amount</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">BroadcastMessage</code> 的 <code class=" language-php">onConnection</code> 和 <code class=" language-php">onQueue</code> 方法:</p><pre class=" language-php"><code class=" language-php"><span class="token keyword">return</span> <span class="token keyword">new</span> <span class="token class-name">BroadcastMessage</span><span class="token punctuation">(</span><span class="token variable">$data</span><span class="token punctuation">)</span>
<span class="token operator">-</span><span class="token operator">></span><span class="token function">onConnection<span class="token punctuation">(</span></span><span class="token string">'sqs'</span><span class="token punctuation">)</span>
<span class="token operator">-</span><span class="token operator">></span><span class="token function">onQueue<span class="token punctuation">(</span></span><span class="token string">'broadcasts'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><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> 除了你指定的数据外,广播通知也包含一个 <code class=" language-php">type</code> 字段,这个字段包含了通知类的类名。</p></blockquote><p><a name="listening-for-notifications"></a></p><h3>监听通知</h3><p>通知将会在一个私有频道里进行广播,频道格式为 <code class=" language-php"><span class="token punctuation">{</span>notifiable<span class="token punctuation">}</span><span class="token punctuation">.</span><span class="token punctuation">{</span>id<span class="token punctuation">}</span></code>。所以,如果你给 ID 为 <code class=" language-php"><span class="token number">1</span></code> 的 <code class=" language-php">App\<span class="token package">User</span></code> 实例发送通知,这个通知就在 <code class=" language-php">App<span class="token punctuation">.</span>User<span class="token number">.1</span></code> 私有频道里被发送。当你使用 <a href="/docs/5.4/broadcasting">Laravel Echo</a> 的时候,你可以很简单地使用 <code class=" language-php">notification</code> 辅助函数来监听一个频道的通知:</p><pre class=" language-php"><code class=" language-php"><span class="token keyword">Echo</span><span class="token punctuation">.</span><span class="token keyword">private</span><span class="token punctuation">(</span><span class="token string">'App.User.'</span> <span class="token operator">+</span> userId<span class="token punctuation">)</span>
<span class="token punctuation">.</span><span class="token function">notification<span class="token punctuation">(</span></span><span class="token punctuation">(</span>notification<span class="token punctuation">)</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token punctuation">{</span>
console<span class="token punctuation">.</span><span class="token function">log<span class="token punctuation">(</span></span>notification<span class="token punctuation">.</span>type<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">receiveBroadcastNotificationsOn</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><span class="token punctuation">;</span>
<span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Notifications<span class="token punctuation">\</span>Notifiable</span><span class="token punctuation">;</span>
<span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Broadcasting<span class="token punctuation">\</span>PrivateChannel</span><span class="token punctuation">;</span>
<span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Foundation<span class="token punctuation">\</span>Auth<span class="token punctuation">\</span>User</span> <span class="token keyword">as</span> Authenticatable<span class="token punctuation">;</span>
<span class="token keyword">class</span> <span class="token class-name">User</span> <span class="token keyword">extends</span> <span class="token class-name">Authenticatable</span>
<span class="token punctuation">{</span>
<span class="token keyword">use</span> <span class="token package">Notifiable</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">/**
* 用户接收的通知广播
*
* @return array
*/</span>
<span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function">receivesBroadcastNotificationsOn<span class="token punctuation">(</span></span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
<span class="token keyword">return</span> <span class="token punctuation">[</span>
<span class="token keyword">new</span> <span class="token class-name">PrivateChannel</span><span class="token punctuation">(</span><span class="token string">'users.'</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">id</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="sms-notifications"></a></p><h2><a href="#sms-notifications">短信通知</a></h2><p><a name="sms-prerequisites"></a></p><h3>先决条件</h3><p>在 Laravel 中发送短信通知是基于 <a href="https://www.nexmo.com/">Nexmo</a> 服务的。在通过 Nexmo 发送短信通知前,你需要安装 <code class=" language-php">nexmo<span class="token operator">/</span>client</code> Composer 包并在 <code class=" language-php">config<span class="token operator">/</span>services<span class="token punctuation">.</span>php</code> 配置文件中添加几个配置选项。你可以复制下面的配置示例来开始使用:</p><pre class=" language-php"><code class=" language-php"><span class="token string">'nexmo'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token punctuation">[</span>
<span class="token string">'key'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token function">env<span class="token punctuation">(</span></span><span class="token string">'NEXMO_KEY'</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
<span class="token string">'secret'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token function">env<span class="token punctuation">(</span></span><span class="token string">'NEXMO_SECRET'</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
<span class="token string">'sms_from'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token string">'15556666666'</span><span class="token punctuation">,</span>
<span class="token punctuation">]</span><span class="token punctuation">,</span></code></pre><p><code class=" language-php">sms_from</code> 选项是短信消息发送者的手机号码。你可以在 Nexmo 控制面板中生成一个手机号码。</p><p><a name="formatting-sms-notifications"></a></p><h3>格式化短信通知</h3><p>如果通知支持以短信方式发送,你应该在通知类里定义一个 <code class=" language-php">toNexmo</code> 方法。这个方法将会收到一个 <code class=" language-php"><span class="token variable">$notifiable</span></code> 实体并返回一个 <code class=" language-php">Illuminate\<span class="token package">Notifications<span class="token punctuation">\</span>Messages<span class="token punctuation">\</span>NexmoMessage</span></code> 实例:</p><pre class=" language-php"><code class=" language-php"><span class="token comment" spellcheck="true">/**
* 获取通知的 Nexmo / 短信展示方式
*
* @param mixed $notifiable
* @return NexmoMessage
*/</span>
<span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function">toNexmo<span class="token punctuation">(</span></span><span class="token variable">$notifiable</span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
<span class="token keyword">return</span> <span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">NexmoMessage</span><span class="token punctuation">)</span>
<span class="token operator">-</span><span class="token operator">></span><span class="token function">content<span class="token punctuation">(</span></span><span class="token string">'Your SMS message content'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span></code></pre><h4>Unicode 内容</h4><p>如果您的 SMS 消息包含 unicode 字符,您应该在构建 <code class=" language-php">NexmoMessage</code> 实例时调用 <code class=" language-php">unicode</code> 方法:</p><pre class=" language-php"><code class=" language-php"><span class="token comment" spellcheck="true">/**
* 获取通知的 Nexmo / 短信展示方式
*
* @param mixed $notifiable
* @return NexmoMessage
*/</span>
<span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function">toNexmo<span class="token punctuation">(</span></span><span class="token variable">$notifiable</span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
<span class="token keyword">return</span> <span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">NexmoMessage</span><span class="token punctuation">)</span>
<span class="token operator">-</span><span class="token operator">></span><span class="token function">content<span class="token punctuation">(</span></span><span class="token string">'Your unicode message'</span><span class="token punctuation">)</span>
<span class="token operator">-</span><span class="token operator">></span><span class="token function">unicode<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span></code></pre><p><a name="customizing-the-from-number"></a></p><h3>自定义 <code class=" language-php">From</code> 号码</h3><p>如果你想通过一个手机号来发送某些通知,而这个手机号不同于你的 <code class=" language-php">config<span class="token operator">/</span>services<span class="token punctuation">.</span>php</code> 配置文件中指定的话,你可以在 <code class=" language-php">NexmoMessage</code> 实例中使用 <code class=" language-php">from</code>:</p><pre class=" language-php"><code class=" language-php"><span class="token comment" spellcheck="true">/**
* 获取通知的 Nexmo / 短信展示方式
*
* @param mixed $notifiable
* @return NexmoMessage
*/</span>
<span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function">toNexmo<span class="token punctuation">(</span></span><span class="token variable">$notifiable</span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
<span class="token keyword">return</span> <span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">NexmoMessage</span><span class="token punctuation">)</span>
<span class="token operator">-</span><span class="token operator">></span><span class="token function">content<span class="token punctuation">(</span></span><span class="token string">'Your SMS message content'</span><span class="token punctuation">)</span>
<span class="token operator">-</span><span class="token operator">></span><span class="token function">from<span class="token punctuation">(</span></span><span class="token string">'15554443333'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span></code></pre><p><a name="routing-sms-notifications"></a></p><h3>路由短信通知</h3><p>当通过 <code class=" language-php">nexmo</code> 频道来发送通知的时候,通知系统会自动寻找通知实体的 <code class=" language-php">phone_number</code> 属性。如果你想自定义通知被发送的手机号码,你要在通知实体里定义一个 <code class=" language-php">routeNotificationForNexmo</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><span class="token punctuation">;</span>
<span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Notifications<span class="token punctuation">\</span>Notifiable</span><span class="token punctuation">;</span>
<span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Foundation<span class="token punctuation">\</span>Auth<span class="token punctuation">\</span>User</span> <span class="token keyword">as</span> Authenticatable<span class="token punctuation">;</span>
<span class="token keyword">class</span> <span class="token class-name">User</span> <span class="token keyword">extends</span> <span class="token class-name">Authenticatable</span>
<span class="token punctuation">{</span>
<span class="token keyword">use</span> <span class="token package">Notifiable</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">/**
* Nexmo 频道的路由通知
*
* @return string
*/</span>
<span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function">routeNotificationForNexmo<span class="token punctuation">(</span></span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
<span class="token keyword">return</span> <span class="token this">$this</span><span class="token operator">-</span><span class="token operator">></span><span class="token property">phone</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token punctuation">}</span></code></pre><p><a name="slack-notifications"></a></p><h2><a href="#slack-notifications">Slack 通知</a></h2><p><a name="slack-prerequisites"></a></p><h3>先决条件</h3><p>通过 Slack 发送通知前,你必须通过 Composer 安装 Guzzle HTTP 库:</p><pre class=" language-php"><code class=" language-php">composer <span class="token keyword">require</span> guzzlehttp<span class="token operator">/</span>guzzle</code></pre><p>你还需要给 Slack 组配置 「Incoming Webhook」 。它将提供你使用 <a href="#routing-slack-notifications">路由 Slack 通知</a> 时用到的 URL。</p><p><a name="formatting-slack-notifications"></a></p><h3>格式化 Slack 通知</h3><p>如果通知支持被当做 Slack 消息发送,你应该在通知类里定义一个 <code class=" language-php">toSlack</code> 方法。这个方法将收到一个 <code class=" language-php"><span class="token variable">$notifiable</span></code> 实体并且返回一个 <code class=" language-php">Illuminate\<span class="token package">Notifications<span class="token punctuation">\</span>Messages<span class="token punctuation">\</span>SlackMessage</span></code> 实例。Slack 消息可以包含文本内容以及一个 「attachment」 用来格式化额外文本或者字段数组。我们来看个基本的 <code class=" language-php">toSlack</code> 例子:</p><pre class=" language-php"><code class=" language-php"><span class="token comment" spellcheck="true">/**
* 获取通知的 Slack 展示方式
*
* @param mixed $notifiable
* @return SlackMessage
*/</span>
<span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function">toSlack<span class="token punctuation">(</span></span><span class="token variable">$notifiable</span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
<span class="token keyword">return</span> <span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">SlackMessage</span><span class="token punctuation">)</span>
<span class="token operator">-</span><span class="token operator">></span><span class="token function">content<span class="token punctuation">(</span></span><span class="token string">'One of your invoices has been paid!'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span></code></pre><p>这个例子中,我们只发送了一行文本给 Slack,这会创建类似下面的一条消息:</p><img src="https://laravel.com/assets/img/basic-slack-notification.png"><h4>自定义发件人和收件人</h4><p>您可以使用 <code class=" language-php">from</code> 和 <code class=" language-php">to</code> 方法来自定义发件人和收件人。 <code class=" language-php">from</code> 方法接受用户名和表情符号标识符,而 <code class=" language-php">to</code> 方法接受一个频道或用户名:</p><pre class=" language-php"><code class=" language-php"><span class="token comment" spellcheck="true">/**
* 获取通知的 Slack 展示方式
*
* @param mixed $notifiable
* @return SlackMessage
*/</span>
<span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function">toSlack<span class="token punctuation">(</span></span><span class="token variable">$notifiable</span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
<span class="token keyword">return</span> <span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">SlackMessage</span><span class="token punctuation">)</span>
<span class="token operator">-</span><span class="token operator">></span><span class="token function">from<span class="token punctuation">(</span></span><span class="token string">'Ghost'</span><span class="token punctuation">,</span> <span class="token string">':ghost:'</span><span class="token punctuation">)</span>
<span class="token operator">-</span><span class="token operator">></span><span class="token function">to<span class="token punctuation">(</span></span><span class="token comment" spellcheck="true">'#other')
</span> <span class="token operator">-</span><span class="token operator">></span><span class="token function">content<span class="token punctuation">(</span></span>'This will be sent to<span class="token comment" spellcheck="true"> #other');
</span><span class="token punctuation">}</span></code></pre><p>你也可以不使用表情符号,改为使用图片作为你的 logo:</p><pre class=" language-php"><code class=" language-php"><span class="token comment" spellcheck="true">/**
* 获取通知的 Slack 展示方式
*
* @param mixed $notifiable
* @return SlackMessage
*/</span>
<span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function">toSlack<span class="token punctuation">(</span></span><span class="token variable">$notifiable</span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
<span class="token keyword">return</span> <span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">SlackMessage</span><span class="token punctuation">)</span>
<span class="token operator">-</span><span class="token operator">></span><span class="token function">from<span class="token punctuation">(</span></span><span class="token string">'Laravel'</span><span class="token punctuation">)</span>
<span class="token operator">-</span><span class="token operator">></span><span class="token function">image<span class="token punctuation">(</span></span><span class="token string">'https://laravel.com/favicon.png'</span><span class="token punctuation">)</span>
<span class="token operator">-</span><span class="token operator">></span><span class="token function">content<span class="token punctuation">(</span></span><span class="token string">'This will display the Laravel logo next to the message'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span></code></pre><p><a name="slack-attachments"></a></p><h3>Slack 附加项 (Attachments)</h3><p>你可以给 Slack 消息添加 "附加项"。附加项提供了比简单文本消息更丰富的格式化选项。在这个例子中,我们将发送一条有关应用中异常的错误通知,它里面有个可以查看这个异常更多详情的链接:</p><pre class=" language-php"><code class=" language-php"><span class="token comment" spellcheck="true">/**
* 获取通知的 Slack 展示方式。
*
* @param mixed $notifiable
* @return SlackMessage
*/</span>
<span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function">toSlack<span class="token punctuation">(</span></span><span class="token variable">$notifiable</span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
<span class="token variable">$url</span> <span class="token operator">=</span> <span class="token function">url<span class="token punctuation">(</span></span><span class="token string">'/exceptions/'</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">exception</span><span class="token operator">-</span><span class="token operator">></span><span class="token property">id</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">return</span> <span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">SlackMessage</span><span class="token punctuation">)</span>
<span class="token operator">-</span><span class="token operator">></span><span class="token function">error<span class="token punctuation">(</span></span><span class="token punctuation">)</span>
<span class="token operator">-</span><span class="token operator">></span><span class="token function">content<span class="token punctuation">(</span></span><span class="token string">'Whoops! Something went wrong.'</span><span class="token punctuation">)</span>
<span class="token operator">-</span><span class="token operator">></span><span class="token function">attachment<span class="token punctuation">(</span></span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token variable">$attachment</span><span class="token punctuation">)</span> <span class="token keyword">use</span> <span class="token punctuation">(</span><span class="token variable">$url</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token variable">$attachment</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">title<span class="token punctuation">(</span></span><span class="token string">'Exception: File Not Found'</span><span class="token punctuation">,</span> <span class="token variable">$url</span><span class="token punctuation">)</span>
<span class="token operator">-</span><span class="token operator">></span><span class="token function">content<span class="token punctuation">(</span></span><span class="token string">'File [background.jpg] was not found.'</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>上面这个例子将会生成一条类似下面的 Slack 消息:</p><img src="https://laravel.com/assets/img/basic-slack-attachment.png"><p>附加项也允许你指定一个应该被展示给用户的数据的数组。给定的数据将会以表格样式展示出来,这能方便阅读:</p><pre class=" language-php"><code class=" language-php"><span class="token comment" spellcheck="true">/**
* 获取通知的 Slack 展示方式
*
* @param mixed $notifiable
* @return SlackMessage
*/</span>
<span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function">toSlack<span class="token punctuation">(</span></span><span class="token variable">$notifiable</span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
<span class="token variable">$url</span> <span class="token operator">=</span> <span class="token function">url<span class="token punctuation">(</span></span><span class="token string">'/invoices/'</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">invoice</span><span class="token operator">-</span><span class="token operator">></span><span class="token property">id</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">return</span> <span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">SlackMessage</span><span class="token punctuation">)</span>
<span class="token operator">-</span><span class="token operator">></span><span class="token function">success<span class="token punctuation">(</span></span><span class="token punctuation">)</span>
<span class="token operator">-</span><span class="token operator">></span><span class="token function">content<span class="token punctuation">(</span></span><span class="token string">'One of your invoices has been paid!'</span><span class="token punctuation">)</span>
<span class="token operator">-</span><span class="token operator">></span><span class="token function">attachment<span class="token punctuation">(</span></span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token variable">$attachment</span><span class="token punctuation">)</span> <span class="token keyword">use</span> <span class="token punctuation">(</span><span class="token variable">$url</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token variable">$attachment</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">title<span class="token punctuation">(</span></span><span class="token string">'Invoice 1322'</span><span class="token punctuation">,</span> <span class="token variable">$url</span><span class="token punctuation">)</span>
<span class="token operator">-</span><span class="token operator">></span><span class="token function">fields<span class="token punctuation">(</span></span><span class="token punctuation">[</span>
<span class="token string">'Title'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token string">'Server Expenses'</span><span class="token punctuation">,</span>
<span class="token string">'Amount'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token string">'$1,234'</span><span class="token punctuation">,</span>
<span class="token string">'Via'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token string">'American Express'</span><span class="token punctuation">,</span>
<span class="token string">'Was Overdue'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token string">':-1:'</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><span class="token punctuation">;</span>
<span class="token punctuation">}</span></code></pre><p>上面的例子将会生成一条类似下面的 Slack 消息:</p><img src="https://laravel.com/assets/img/slack-fields-attachment.png"><h4>Markdown 附件内容</h4><p>如果一些附件字段包含 Markdown ,您可以使用 <code class=" language-php">markdown</code> 方法指示 Slack 解析并将给定的附件字段显示为 Markdown 格式的文本:</p><pre class=" language-php"><code class=" language-php"><span class="token comment" spellcheck="true">/**
* 获取通知的 Slack 展示方式
*
* @param mixed $notifiable
* @return SlackMessage
*/</span>
<span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function">toSlack<span class="token punctuation">(</span></span><span class="token variable">$notifiable</span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
<span class="token variable">$url</span> <span class="token operator">=</span> <span class="token function">url<span class="token punctuation">(</span></span><span class="token string">'/exceptions/'</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">exception</span><span class="token operator">-</span><span class="token operator">></span><span class="token property">id</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">return</span> <span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">SlackMessage</span><span class="token punctuation">)</span>
<span class="token operator">-</span><span class="token operator">></span><span class="token function">error<span class="token punctuation">(</span></span><span class="token punctuation">)</span>
<span class="token operator">-</span><span class="token operator">></span><span class="token function">content<span class="token punctuation">(</span></span><span class="token string">'Whoops! Something went wrong.'</span><span class="token punctuation">)</span>
<span class="token operator">-</span><span class="token operator">></span><span class="token function">attachment<span class="token punctuation">(</span></span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token variable">$attachment</span><span class="token punctuation">)</span> <span class="token keyword">use</span> <span class="token punctuation">(</span><span class="token variable">$url</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token variable">$attachment</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">title<span class="token punctuation">(</span></span><span class="token string">'Exception: File Not Found'</span><span class="token punctuation">,</span> <span class="token variable">$url</span><span class="token punctuation">)</span>
<span class="token operator">-</span><span class="token operator">></span><span class="token function">content<span class="token punctuation">(</span></span><span class="token string">'File [background.jpg] was **not found**.'</span><span class="token punctuation">)</span>
<span class="token operator">-</span><span class="token operator">></span><span class="token function">markdown<span class="token punctuation">(</span></span><span class="token punctuation">[</span><span class="token string">'title'</span><span class="token punctuation">,</span> <span class="token string">'text'</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>
<span class="token punctuation">}</span></code></pre><p><a name="routing-slack-notifications"></a></p><h3>路由 Slack 通知</h3><p>要把 Slack 通知路由到正确的位置,你要在通知实体中定义 <code class=" language-php">routeNotificationForSlack</code> 方法。它返回通知要被发往的 URL 回调地址。URL 可以通过在 Slack 组里面添加 "Incoming Webhook" 服务来生成。</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><span class="token punctuation">;</span>
<span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Notifications<span class="token punctuation">\</span>Notifiable</span><span class="token punctuation">;</span>
<span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Foundation<span class="token punctuation">\</span>Auth<span class="token punctuation">\</span>User</span> <span class="token keyword">as</span> Authenticatable<span class="token punctuation">;</span>
<span class="token keyword">class</span> <span class="token class-name">User</span> <span class="token keyword">extends</span> <span class="token class-name">Authenticatable</span>
<span class="token punctuation">{</span>
<span class="token keyword">use</span> <span class="token package">Notifiable</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">/**
* Slack 频道的通知路由
*
* @return string
*/</span>
<span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function">routeNotificationForSlack<span class="token punctuation">(</span></span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
<span class="token keyword">return</span> <span class="token this">$this</span><span class="token operator">-</span><span class="token operator">></span><span class="token property">slack_webhook_url</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token punctuation">}</span></code></pre><p><a name="notification-events"></a></p><h2><a href="#notification-events">通知事件</a></h2><p>当通知发送后,通知系统就会触发 <code class=" language-php">Illuminate\<span class="token package">Notifications<span class="token punctuation">\</span>Events<span class="token punctuation">\</span>NotificationSent</span></code> 事件。它包含了 「notifiable」 实体和通知实例本身。你应该在 <code class=" language-php">EventServiceProvider</code> 中注册监听器:</p><pre class=" language-php"><code class=" language-php"><span class="token comment" spellcheck="true">/**
* 应用中的事件监听映射
*
* @var array
*/</span>
<span class="token keyword">protected</span> <span class="token variable">$listen</span> <span class="token operator">=</span> <span class="token punctuation">[</span>
<span class="token string">'Illuminate\Notifications\Events\NotificationSent'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token punctuation">[</span>
<span class="token string">'App\Listeners\LogNotification'</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><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> 注册完监听器后,使用 <code class=" language-php">event<span class="token punctuation">:</span>generate</code> Artisan 命令来快速生成监听器类。</p></blockquote><p>在事件监听器中,你可以访问事件中的 <code class=" language-php">notifiable</code>, <code class=" language-php">notification</code>, 和 <code class=" language-php">channel</code> 属性,来了解通知接收者和通知本身:</p><pre class=" language-php"><code class=" language-php"><span class="token comment" spellcheck="true">/**
* 处理事件
*
* @param NotificationSent $event
* @return void
*/</span>
<span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function">handle<span class="token punctuation">(</span></span>NotificationSent <span class="token variable">$event</span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
<span class="token comment" spellcheck="true"> // $event->channel
</span> <span class="token comment" spellcheck="true"> // $event->notifiable
</span> <span class="token comment" spellcheck="true"> // $event->notification
</span><span class="token punctuation">}</span></code></pre><p><a name="custom-channels"></a></p><h2><a href="#custom-channels">自定义频道</a></h2><p>Laravel 提供了开箱即用的通知频道,但是你可能会想编写自己的驱动来通过其他频道发送通知。Laravel 很容易实现。首先,定义一个包含 <code class=" language-php">send</code> 方法的类。这个方法应该收到两个参数:<code class=" language-php"><span class="token variable">$notifiable</span></code> 和 <code class=" language-php"><span class="token variable">$notification</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>Channels</span><span class="token punctuation">;</span>
<span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Notifications<span class="token punctuation">\</span>Notification</span><span class="token punctuation">;</span>
<span class="token keyword">class</span> <span class="token class-name">VoiceChannel</span>
<span class="token punctuation">{</span>
<span class="token comment" spellcheck="true">/**
* 发送给定通知
*
* @param mixed $notifiable
* @param \Illuminate\Notifications\Notification $notification
* @return void
*/</span>
<span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function">send<span class="token punctuation">(</span></span><span class="token variable">$notifiable</span><span class="token punctuation">,</span> Notification <span class="token variable">$notification</span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
<span class="token variable">$message</span> <span class="token operator">=</span> <span class="token variable">$notification</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">toVoice<span class="token punctuation">(</span></span><span class="token variable">$notifiable</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true"> // 将通知发送给 $notifiable 实例
</span> <span class="token punctuation">}</span>
<span class="token punctuation">}</span></code></pre><p>一旦定义了通知频道类,你应该在所有通知里通过 <code class=" language-php">via</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>Notifications</span><span class="token punctuation">;</span>
<span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Bus<span class="token punctuation">\</span>Queueable</span><span class="token punctuation">;</span>
<span class="token keyword">use</span> <span class="token package">App<span class="token punctuation">\</span>Channels<span class="token punctuation">\</span>VoiceChannel</span><span class="token punctuation">;</span>
<span class="token keyword">use</span> <span class="token package">App<span class="token punctuation">\</span>Channels<span class="token punctuation">\</span>Messages<span class="token punctuation">\</span>VoiceMessage</span><span class="token punctuation">;</span>
<span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Notifications<span class="token punctuation">\</span>Notification</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>Queue<span class="token punctuation">\</span>ShouldQueue</span><span class="token punctuation">;</span>
<span class="token keyword">class</span> <span class="token class-name">InvoicePaid</span> <span class="token keyword">extends</span> <span class="token class-name">Notification</span>
<span class="token punctuation">{</span>
<span class="token keyword">use</span> <span class="token package">Queueable</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">/**
* 获取通知频道
*
* @param mixed $notifiable
* @return array|string
*/</span>
<span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function">via<span class="token punctuation">(</span></span><span class="token variable">$notifiable</span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
<span class="token keyword">return</span> <span class="token punctuation">[</span><span class="token scope">VoiceChannel<span class="token punctuation">::</span></span><span class="token keyword">class</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token comment" spellcheck="true">/**
* 获取通知的声音展示方式
*
* @param mixed $notifiable
* @return VoiceMessage
*/</span>
<span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function">toVoice<span class="token punctuation">(</span></span><span class="token variable">$notifiable</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><h2>译者署名</h2><table><thead><tr><th>用户名</th><th>头像</th><th>职能</th><th>签名</th></tr></thead><tbody><tr><td><a href="https://phphub.org/users/11602">@司维</a></td><td><img class="avatar-66 rm-style" src="https://dn-phphub.qbox.me/uploads/avatars/11602_1487572457.png?imageView2/1/w/380/h/380"></td><td>翻译</td><td> <a href="https://github.com/leon0204">@Leon0204</a> at Github</td></tr></tbody></table></article>
- 入门指南
- 安装
- 配置信息
- 文件夹结构
- 请求周期
- 开发环境部署
- Valet
- Homestead
- 核心概念
- 服务提供者
- Facades
- Contracts
- 服务容器
- HTTP 层
- 路由
- 中间件
- CSRF 保护
- 控制器
- 请求
- 响应
- 视图
- Session
- 表单验证
- 前端
- Blade 模板
- 本地化
- 前端指南
- 编辑资源 Mix
- 安全
- API 认证
- 用户认证
- 用户授权
- 加密解密
- 哈希
- 重置密码
- 数据库
- 快速入门
- 查询构造器
- 分页
- 数据库迁移
- Redis
- 数据填充
- Eloquent ORM
- Eloquent ORM快速入门
- 模型关联
- Eloquent 集合
- 修改器
- 序列化
- 综合话题
- Artisan 命令行
- 广播系统
- 缓存系统
- 集合
- 错误与日志
- 事件系统
- 文件存储
- 辅助函数
- 邮件发送
- 消息通知
- 扩展包开发
- 队列
- 任务调度