?
有時候當我們單純的看Laravel
手冊的時候會有一些疑惑,比如說系統服務下的授權和事件,這些功能服務的應用場景是什麼,其實如果沒有經歷過一定的開發經驗有這些疑惑是很正常的事情,但是當我們在工作中多加思考會發現有時候這些服務其實我們一直都看過。下面就事件、事件監聽舉一個很簡單的例子你就會發現。
? 這個例子是關於文章的瀏覽數的實現,當用戶查看文章的時候文章的瀏覽數會增加1,用戶查看文章就是一個事件,有了事件,就需要一個事件監聽器,對監聽的事件發生後執行對應的操作(文章瀏覽數加1),其實這種監聽機制在Laravel
中是透過觀察者模式實現的.
註冊事件以及監聽器
首先我們需要在app/Providers/
目錄下的EventServiceProvider.php
中註冊事件監聽器映射關係,如下:
<code><span>protected</span><span>$listen</span> = <span>[</span><span>'App\Events\BlogView'</span> => <span>[</span><span>'App\Listeners\BlogViewListener'</span><span>,</span><span>],</span><span>];</span></code>
接著專案根目錄下執行如下指令
<code>php artisan event:generate</code>
接著專案根目錄下執行如下指令reee /Events
和
app/Listensers目錄下產生
BlogView.php和
BlogViewListener.php
<code><span><?php</span><span>namespace</span> App\Events<span>;</span><span>use</span> App\Events\Event<span>;</span><span>use</span> App\Post<span>;</span><span>use</span> Illuminate\Queue\SerializesModels<span>;</span><span>use</span> Illuminate\Contracts\Broadcasting\ShouldBroadcast<span>;</span><span>class</span> BlogView <span>extends</span> Event { <span>use</span> SerializesModels<span>;</span><span>/**</span><span> * Create a new event instance.</span><span> *</span><span> * </span><span>@return</span><span> void</span><span> */</span><span>public</span><span>function</span><span>__construct</span><span>(</span>Post <span>$post</span><span>)</span> { <span>$this</span>->post = <span>$post</span><span>;</span> } <span>/**</span><span> * Get the channels the event should be broadcast on.</span><span> *</span><span> * </span><span>@return</span><span> array</span><span> */</span><span>public</span><span>function</span> broadcastOn<span>()</span> { <span>return</span><span>[];</span> } }</span></code>
其實看到這些你會發現該事件類只是注入了一個
Post
事件監聽器在
handle方法中接收事件實例,event:generate指令將會自動在handle方法中匯入適當的事件類別和類型提示事件。在
handle
<code><span><?php</span><span>namespace</span> App\Listeners<span>;</span><span>use</span> App\Events\BlogView<span>;</span><span>use</span> Illuminate\Queue\InteractsWithQueue<span>;</span><span>use</span> Illuminate\Contracts\Queue\ShouldQueue<span>;</span><span>use</span> Illuminate\Session\Store<span>;</span><span>class</span> BlogViewListener { <span>protected</span><span>$session</span><span>;</span><span>/**</span><span> * Create the event listener.</span><span> *</span><span> * </span><span>@return</span><span> void</span><span> */</span><span>public</span><span>function</span><span>__construct</span><span>(</span>Store <span>$session</span><span>)</span> { <span>$this</span>->session = <span>$session</span><span>;</span> } <span>/**</span><span> * Handle the event.</span><span> *</span><span> * </span><span>@param</span><span>BlogView</span><span> $event</span><span> * </span><span>@return</span><span> void</span><span> */</span><span>public</span><span>function</span> handle<span>(</span>BlogView <span>$event</span><span>)</span> { <span>$post</span> = <span>$event</span>->post<span>;</span><span>//先进行判断是否已经查看过</span><span>if</span><span>(</span>!<span>$this</span>->hasViewedBlog<span>(</span><span>$post</span><span>))</span> { <span>//保存到数据库</span><span>$post</span>->view_cache = <span>$post</span>->view_cache + <span>1</span><span>;</span><span>$post</span>->save<span>();</span><span>//看过之后将保存到 Session </span><span>$this</span>->storeViewedBlog<span>(</span><span>$post</span><span>);</span> } } <span>protected</span><span>function</span> hasViewedBlog<span>(</span><span>$post</span><span>)</span> { <span>return</span><span>array_key_exists</span><span>(</span><span>$post</span>->id<span>,</span><span>$this</span>->getViewedBlogs<span>());</span> } <span>protected</span><span>function</span> getViewedBlogs<span>()</span> { <span>return</span><span>$this</span>->session->get<span>(</span><span>'viewed_Blogs'</span><span>,</span><span>[]);</span> } <span>protected</span><span>function</span> storeViewedBlog<span>(</span><span>$post</span><span>)</span> { <span>$key</span> = <span>'viewed_Blogs.'</span>.<span>$post</span>->id<span>;</span><span>$this</span>->session->put<span>(</span><span>$key</span><span>,</span><span>time</span><span>());</span> } }</span></code>註解中也已經說明了一些邏輯。 觸發事件
事件和事件監聽完成後,我們要做的就是實現整個監聽,即觸發用戶打開文章事件在此我們使用和
Event提供的
fire
以上就介紹了Laravel 51 事件、事件監聽的簡單應用,包括了開發經驗,映射關係方面的內容,希望對PHP教程有興趣的朋友有所幫助。