码迷,mamicode.com
首页 > 其他好文 > 详细

Lumen/laravel Event Demo

时间:2016-04-01 00:53:51      阅读:303      评论:0      收藏:0      [点我收藏+]

标签:

最近在学习lumen,想用它来编写接口,学习过程中做点记录;

 

首先,按照官方介绍,创建 lumen project:

composer create-project --prefer-dist laravel/lumen lumenEventDemo

  

项目创建完成,下面进入正题;

事件,通常来讲事件就是某种行为,laravel给我们定义了一个简单的模型,事件包含事件本身,订阅、监听器以及事件的发起;

按照上面的概念,我们来所以下定义:

事件本身(事件名称):一边来讲是以某个动作发生为名词,比如:OnClieck,OnCliecked,OnUserCreated,OnDbError等, 这里举例为:ExampleEvent;

事件监听器:就是一个简单事件用来,可用来做一些代码处理,这里我们叫:ExampleListener;

 

好的,基础工作确定好了,那么就开始编程呗;

首先定义好事件,在“App\Events\"下,创建文件"ExampleEvent.php",并编写如下代码:

<?php

namespace App\Events;

class ExampleEvent extends Event
{
    public $data;
    /**
     * Create a new event instance.
     *
     * @return void
     */
    public function __construct($data)
    {
        //
        $this->data=$data;
    }
}

  

这样,我们就编写好一个事件原型;

然后,我们需要能够发起事件,我这里就直接在Controller发起,在”App\Http\Controllers” 下创建名为 "ExamplerController",并编写一个方法叫 message,代码如下:

<?php

namespace App\Http\Controllers;

use App\Events\ExampleEvent;
 
class ExampleController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    public function message(){
        //TODO SOMETHING;

        $data = array(
            "hi"=>‘Lingfeng‘,
            "world"=>"Chen"
        );

        event(new ExampleEvent($data));  //这里发起事件
    }

    //
}

  

好的,现在从代码角度来看,事件也有了,message function也触发了事件,那么事件发起后,谁监听、谁来用呢?

这时候得从启动项中找到点东东:

编辑/bootstrap/app.php,去掉register EventServiceProvider的注释;

$app->register(App\Providers\EventServiceProvider::class);

  

这样,EventServiceProvider 就能够使用了,但ExampleEvent依然没有谁来监听;

好的,在”App\Listeners\"下创建 "ExampleListener",并编写如下代码:

<?php

namespace App\Listeners;

use App\Events\ExampleEvent;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\Listener;

class ExampleListener extends Listener
{
    /**
     * Create the event listener.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Handle the event.
     *
     * @param  ExampleEvent  $event
     * @return void
     */
    public function handle(ExampleEvent $event)
    {
        //
        info("example listener");
var_dump($event);
}
}

  

在Handle中可以对event做各种操作了;

但事情没有结束,因为看起来代码都有了,可是依然没有任何一个地方讲 Event和Listener做关联;这个关联就在EventServiceProvider中

修改Provider代码如下:

技术分享
<?php

namespace App\Providers;

use Laravel\Lumen\Providers\EventServiceProvider as ServiceProvider;

class EventServiceProvider extends ServiceProvider
{
    /**
     * The event listener mappings for the application.
     *
     * @var array
     */
    protected $listen = [
        ‘App\Events\ExampleEvent‘ => [
            ‘App\Listeners\ExampleListener‘,
        ],
    ];
}
EventServiceProvider

 

至此,大功告成;

配置好域名后,访问地址Http://localhost/example/message 

页面输出:

技术分享
object(App\Events\ExampleEvent)[22]
  public ‘data‘ => 
    array (size=2)
      ‘hi‘ => string ‘Lingfeng‘ (length=8)
      ‘world‘ => string ‘Chen‘ (length=4)
ExampleEvent

 

同时检查log(storage\logs\lumen.log)

技术分享
[2016-03-31 15:12:08] lumen.INFO: example listener
log

 

但如果我们使用了数据库,还需要配置上jobs 表:

技术分享
<?php 

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateJobs extends Migration
{
    /**
     * Run the migrations.
     */
    public function up()
    {
        Schema::create(‘jobs‘, function(Blueprint $table)
        {
            $table->bigIncrements(‘id‘);
            $table->string(‘queue‘);
            $table->text(‘payload‘);
            $table->tinyInteger(‘attempts‘)->unsigned();
            $table->tinyInteger(‘reserved‘)->unsigned();
            $table->unsignedInteger(‘reserved_at‘)->nullable();
            $table->unsignedInteger(‘available_at‘);
            $table->unsignedInteger(‘created_at‘);
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down()
    {
        Schema::drop(‘jobs‘);
    }
}
Migrate Jobs

 


至此event 实现;再梳理下event相关流程:

HttpRequest ->

bootstarp/app.app  :   register/EventServiceProvider  map Event <-->Listener  ->

ExamplerController:  message  -> event(new ExampleEvent))->

ExamplerListener: handler->todo something;

 

如果希望同时注册多个方法做解析,可以采用订阅的方式:这时候需要做一些改造

技术分享
<?php

namespace App\Events;

use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Queue\SerializesModels;

class ExampleEvent extends Event implements ShouldBroadcast
{
    public $data;
    /**
     * Create a new event instance.
     *
     * @return void
     */
    public function __construct($data)
    {
        //
        $this->data=$data;
    }

    /**
     * Get the channels the event should broadcast on.
     *
     * @return array
     */
    public function broadcastOn()
    {
        // TODO: Implement broadcastOn() method.
        return [‘Example.message‘]; //广播,同时会携带上当前事件中的所有属性,这里是 $this->data;
    }
}
ExampleEvent
技术分享
<?php

namespace App\Listeners;

use App\Events\ExampleEvent;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\Listener;

class ExampleListener
{
    public function onMessage($event){
        echo "onMessage "."<br />";
        var_dump($event);
    }
    public function onMessage1($event){
        echo "onMessage1 "."<br />";
        var_dump($event);
    }
    /**
     * 为订阅者注册监听器
     *
     * @param \Illuminate\Contracts\Events\Dispatcher $events
     * @return array
     */
    public function subscribe($events)
    {

        $events->listen(
            ‘App\Events\ExampleEvent‘,
            ‘App\Listeners\ExampleListener@onMessage‘
        );
        $events->listen(
            ‘App\Events\ExampleEvent‘,
            ‘App\Listeners\ExampleListener@onMessage1‘
        );
    }
}
ExampleListener
技术分享
<?php

namespace App\Providers;

use App\Listeners\ExampleListener;
use Laravel\Lumen\Providers\EventServiceProvider as ServiceProvider;

class EventServiceProvider extends ServiceProvider
{
    ///**
    // * The event listener mappings for the application.
    // *
    // * @var array
    // */
    //protected $listen = [
    //    ‘App\Events\ExampleEvent‘ => [
    //        ‘App\Listeners\ExampleListener‘,
    //    ],
    //];
    protected $subscribe =[
        ExampleListener::class
    ];
}
EventServiceProvider

 

执行代码,应当获得2分数据 并有 onMessage / onMessage1 输出

 

Lumen/laravel Event Demo

标签:

原文地址:http://www.cnblogs.com/lingfengchencn/p/5343390.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!