首頁 >後端開發 >php教程 >Laravel 51 事件、事件監聽的簡單應用

Laravel 51 事件、事件監聽的簡單應用

WBOY
WBOY原創
2016-07-29 09:11:22951瀏覽

?
Laravel 51 事件、事件监听的简单应用

有時候當我們單純的看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 /Eventsapp/Listensers目錄下產生BlogView.phpBlogViewListener.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

方法,如下:

rrrereee現在開啟頁面發現資料庫中的`view_cache已經正常加1了,這樣整個就完成了。

以上就介紹了Laravel 51 事件、事件監聽的簡單應用,包括了開發經驗,映射關係方面的內容,希望對PHP教程有興趣的朋友有所幫助。

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn