zoukankan      html  css  js  c++  java
  • laravel队列,事件简单使用方法

    A.队列的使用

    1.队列配置文件存储在 config/queue.php 根据自己的情况进行配置

    2..env文件 QUEUE_DRIVER=database(根据个人情况配置,redis等)

    3.创建jobs表(不用数据库的可以不用建表)

    php artisan queue:table
    php artisan migrate

    4.创建任务文件
    php artisan make:job Testqueue
    会生成目录和文件appJobsTestqueue.php
    namespace AppJobs;
    
    use IlluminateBusQueueable;
    use IlluminateQueueSerializesModels;
    use IlluminateQueueInteractsWithQueue;
    use IlluminateContractsQueueShouldQueue;
    use IlluminateFoundationBusDispatchable;
    use IlluminateSupportFacadesDB;
    
    class testqueue implements ShouldQueue
    {
        use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
    
        /**
         * Create a new job instance.
         *
         * @return void
         */
        public function __construct()
        {
            //
        }
    
        /**
         * Execute the job.
         *
         * @return void
         */
        public function handle()//handler里就是写业务逻辑的地方了,这里可以用type-hint依赖注入的方式,注入任何你需要的类。
        {
            //
            DB::table('tests')->insert(['name'=>'测试名字']);//这里是自己写代码
        }

    5.自己建一个控制器

    里面写代码

        public function login(Request $request){
            $this->dispatch(new testqueue());
    

     访问此控制器后,在jobs中会有一条队列的记录

    启动队列

    php artisan queue:work
    会把jobs表中数据删除 在tests表中增加一条记录
    B.事件的使用
    Laravel 的事件提供了一个简单的观察者实现,能够订阅和监听应用中发生的各种事件。事件类保存在 app/Events 目录中,而这些事件的的监听器则被保存在 app/Listeners 目录下。这些目录只有当你使用 Artisan 命令来生成事件和监听器时才会被自动创建。

    注册事件和监听器

    Laravel 应用中的 EventServiceProvider 有个 listen 数组包含所有的事件(键)以及事件对应的监听器(值)来注册所有的事件监听器,可以灵活地根据需求来添加事件。例如,让我们增加一个 OrderShipped 事件:

    /**
     * 应用程序的事件监听器映射。
     *
     * @var array
     */
    protected $listen = [
        'AppEventsOrderShipped' => [
            'AppListenersSendShipmentNotification',
        ],
    ];

    生成事件 & 监听器

    php artisan event:generate
    会生成AppEvents目录和OrderShipped.php
    同时生成AppListenersSendShipmentNotification.php

    手动注册事件

    事件通常是在 EventServiceProvider 类的 $listen 数组中注册,但是,你也可以在 EventServiceProvider 类的 boot 方法中注册基于事件的闭包:

    /**
     * 注册应用程序中的任何其它事件。
     *
     * @return void
     */
    public function boot()
    {
        parent::boot();
    
        Event::listen('event.name', function ($foo, $bar) {
            //
        });
    }

    通配符事件监听器

    你可以在注册监听器时使用 * 通配符参数,这样能够在同一个监听器上捕获多个事件。通配符监听器接受事件名称作为其第一个参数,并将整个事件数据数组作为其第二个参数:

    Event::listen('event.*', function ($eventName, array $data) {
        //
    });

    其他操作同队列一样
    C.

    事件订阅(Event Subscribers)

    Event Subscribers是一种特殊的Listener,前面讲的是一个listener里只能放一个hander(),事件订阅可以把很多处理器(handler)放到一个类里面,然后用一个listner把它们集合起来,这样不同的事件只要对应一个listner就可以了。

        <?php
        namespace AppListeners;
        class UserEventListener
        {
            /**
             * Handle user login events.
             */
            public function onUserLogin($event) {}
            /**
             * Handle user logout events.
             */
            public function onUserLogout($event) {}
            /**
             * Register the listeners for the subscriber.
             *
             * @param  IlluminateEventsDispatcher  $events
             * @return array
             */
            public function subscribe($events)
            {
                $events->listen(
                    'AppEventsUserLoggedIn',
                    'AppListenersUserEventListener@onUserLogin'
                );
                $events->listen(
                    'AppEventsUserLoggedOut',
                    'AppListenersUserEventListener@onUserLogout'
                );
            }
        }

    看后面的subscribe(),每个事件和处理器是一一对应的。

    绑定 Event Subscriber到Service Provider

        <?php
        namespace AppProviders;
        use IlluminateContractsEventsDispatcher as DispatcherContract;
        use IlluminateFoundationSupportProvidersEventServiceProvider as ServiceProvider;
        class EventServiceProvider extends ServiceProvider
        {
            /**
             * The event listener mappings for the application.
             *
             * @var array
             */
            protected $listen = [
                //
            ];
            /**
             * The subscriber classes to register.
             *
             * @var array
             */
            protected $subscribe = [
                'AppListenersUserEventListener',
            ];
        }


  • 相关阅读:
    WPF获取分辨率2
    怎样将UNIX Shell作为Concurrent Program来运行
    转 FRM40654 Record has been updated Requery block to see change
    各模组相关interface
    EBS 表后缀的含义
    Oracle Form開發Form消息提示
    EBS多组织(OU
    EBS 开发基础知识
    FORM:在不同窗口中传递参数
    AR 金额计算
  • 原文地址:https://www.cnblogs.com/zuochuang/p/8963258.html
Copyright © 2011-2022 走看看