首页 >后端开发 >php教程 >Laravel 51 事件、事件监听的简单应用

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

WBOY
WBOY原创
2016-07-29 09:11:22949浏览

?
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>

该命令完成后,会分别自动在 app/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方法,如下:

<code><span><?php </span><span>namespace</span> App\Http\Controllers<span>;</span><span>use</span> Illuminate\Http\Request<span>;</span><span>use</span> App\Post<span>;</span><span>use</span> Illuminate\Support\Facades\Event<span>;</span><span>use</span> App\Http\Requests<span>;</span><span>use</span> App\Events\BlogView<span>;</span><span>use</span> App\Http\Controllers\Controller<span>;</span><span>class</span> BlogController <span>extends</span> Controller
{
   
    <span>public</span><span>function</span> showPost<span>(</span><span>$slug</span><span>)</span>
    {
        <span>$post</span> = Post::whereSlug<span>(</span><span>$slug</span><span>)</span>->firstOrFail<span>();</span>
        Event::fire<span>(</span><span>new</span> BlogView<span>(</span><span>$post</span><span>));</span><span>return</span> view<span>(</span><span>'home.blog.content'</span><span>)</span>->withPost<span>(</span><span>$post</span><span>);</span>
    }

}</span></code>

现在打开页面发现数据库中的`view_cache已经正常加1了,这样整个就完成了。

以上就介绍了Laravel 51 事件、事件监听的简单应用,包括了开发经验,映射关系方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn