在本文中,我們將探討Laravel事件管理的基礎知識。我們還將創建一個自定義事件和偵聽器的現實世界示例。
> Laravel中事件的概念基於非常流行的軟件設計模式 - 觀察者模式。在這種模式下,系統會在發生某些事情時提出事件,您可以定義聽取這些事件並做出相應反應的聽眾。這是一個非常有用的功能,使您可以將組件在系統中解除否則會導致緊密耦合代碼的組件。因此,它使他們能夠對此登錄事件做出反應,無論是要發送電子郵件還是應用程序內通知,還是想對此登錄事件做出反應的任何事物。
>事件的基礎知識和聽眾
>同樣,Laravel提供了一個內置的 類,使我們能夠為應用程序定義了everter mappings for Application for Application。 > app/provist/experterviceProvider.phpfile。 >讓我們仔細查看登錄 當然,您需要定義Artisan >> >
>應該已經在app/event/events/clearcache.php app/listerers/thampcache.php 助手函數,用於從應用程序中的任何地方提出事件。當事件提出時,Laravel調用所有聽眾聽那個特定事件的聽眾。因此,<?php<br><br>namespace App\Providers;<br><br>use Illuminate\Auth\Events\Registered;<br>use Illuminate\Auth\Listeners\SendEmailVerificationNotification;<br>use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;<br>use Illuminate\Support\Facades\Event;<br><br>class EventServiceProvider extends ServiceProvider<br>{<br> /**<br> * The event listener mappings for the application.<br> *<br> * @var array<br> */<br> protected $listen = [<br> Registered::class => [<br> SendEmailVerificationNotification::class,<br> ],<br> ];<br><br> /**<br> * Register any events for your application.<br> *<br> * @return void<br> */<br> public function boot()<br> {<br> parent::boot();<br><br> //<br> }<br>}<br>
> event。 php artisan event:generate<br>
php artisan event:generate<br>
<?php<br><br>namespace App\Events;<br><br>use Illuminate\Broadcasting\Channel;<br>use Illuminate\Broadcasting\InteractsWithSockets;<br>use Illuminate\Broadcasting\PresenceChannel;<br>use Illuminate\Broadcasting\PrivateChannel;<br>use Illuminate\Contracts\Broadcasting\ShouldBroadcast;<br>use Illuminate\Foundation\Events\Dispatchable;<br>use Illuminate\Queue\SerializesModels;<br><br>class ClearCache<br>{<br> use Dispatchable, InteractsWithSockets, SerializesModels;<br><br> public $cache_keys = [];<br><br> /**<br> * Create a new event instance.<br> *<br> * @return void<br> */<br> public function __construct(Array $cache_keys)<br> {<br> $this->cache_keys = $cache_keys;<br> }<br><br> /**<br> * Get the channels the event should broadcast on.<br> *<br> * @return \Illuminate\Broadcasting\Channel|array<br> */<br> public function broadcastOn()<br> {<br> return new PrivateChannel('channel-name');<br> }<br>}<br>
AppListenersWarmUpCache
。這是:AppEventsClearCache
handle
AppListenersWarmUpCache
handle
applistenerswarmupcache>從控制器提出事件時,請調用<code> applistenerswarmupcache 偵聽器。剩下的是要熱身緩存已清除的緩存! >,這就是您可以在應用程序中創建自定義事件並與之合作的方式。 ><h2>什麼是事件訂閱者? </h2>
<p>>事件訂閱者允許您在一個地方訂閱多個事件聽眾。無論您是要邏輯上的事件聽眾還是要在一個地方包含成長的事件,都是您要尋找的事件訂閱者。 </p>方法的第一個參數是<p>類的實例,您可以使用</p>>方法來與聽眾綁定事件。 <pre class="brush:php;toolbar:false"><?php<br><br>namespace App\Providers;<br><br>use Illuminate\Auth\Events\Registered;<br>use Illuminate\Auth\Listeners\SendEmailVerificationNotification;<br>use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;<br>use Illuminate\Support\Facades\Event;<br><br>class EventServiceProvider extends ServiceProvider<br>{<br> /**<br> * The event listener mappings for the application.<br> *<br> * @var array<br> */<br> protected $listen = [<br> Registered::class => [<br> SendEmailVerificationNotification::class,<br> ],<br> ];<br><br> /**<br> * Register any events for your application.<br> *<br> * @return void<br> */<br> public function boot()<br> {<br> parent::boot();<br><br> //<br> }<br>}<br></pre>><p>><code>subscribe
subscribe
IlluminateEventsDispatcher
>>方法的第一個參數是您想要收聽的事件,第二個參數是一個偵聽器,當事件被啟動時,可以召集cmirer。本身。 listen
屬性下的“
>$subscribe
>
php artisan event:generate<br>
以上是Laravel的定制活動的詳細內容。更多資訊請關注PHP中文網其他相關文章!