Laravelキュー、イベントの簡単な使用方法
10833 ワード
A.キューの使用
1.キュープロファイルが
2..EnvファイルQUEUE_DRIVER=database(個人の状況に応じて配置、redisなど)
3.jobsテーブルの作成(データベースを使用しないものはテーブルを構築しなくてもよい)
5.自分でコントローラを作る
中にコードを書く
このコントローラにアクセスするとjobsにキューのレコードが表示されます
キューの開始
イベントとListenerの登録
Laravelアプリケーションの
イベント&リスナーの生成
手動登録イベント
イベントは通常、
ワイルドカードイベントリスナー
リスナーを登録するときに
イベント購読(Event Subscribers)
Event Subscribersは特殊なListenerで、前に述べたのは1つのlistenerに1つのhander()しか入れられません.イベントサブスクリプションは多くのプロセッサ(handler)を1つのクラスに入れて、1つのlistnerでそれらを集めることができます.このように異なるイベントは1つのlistnerに対応すればいいです.
後ろのsubscribe()を見ると、各イベントとプロセッサが1つずつ対応しています.
Event SubscriberをService Providerにバインド
転載先:https://www.cnblogs.com/zuochuang/p/8963258.html
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
app\Jobs\Testqueue.php
namespace App\Jobs;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Support\Facades\DB;
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 。
イベントとListenerの登録
Laravelアプリケーションの
EventServiceProvider
には、すべてのイベント(キー)およびイベントに対応するリスナー(値)を含むlisten
配列があり、必要に応じてイベントを柔軟に追加できます.たとえば、OrderShipped
イベントを追加します./**
* 。
*
* @var array
*/
protected $listen = [ 'App\Events\OrderShipped' => [ 'App\Listeners\SendShipmentNotification', ], ];
イベント&リスナーの生成
php artisan event:generate
App\Events\ OrderShipped
.php
App\Listeners\SendShipmentNotification.php
手動登録イベント
イベントは通常、
EventServiceProvider
クラスの$listen
配列に登録されますが、EventServiceProvider
クラスのboot
メソッドにイベントベースの閉パッケージを登録することもできます./**
* 。
*
* @return void
*/
public function boot() { parent::boot(); Event::listen('event.name', function ($foo, $bar) { // }); }
ワイルドカードイベントリスナー
リスナーを登録するときに
*
ワイルドカードパラメータを使用すると、同じリスナーで複数のイベントをキャプチャできます.ワイルドカードリスナーは、イベント名を最初のパラメータとして受け入れ、イベントデータ配列全体を2番目のパラメータとして受け入れます.Event::listen('event.*', function ($eventName, array $data) { // });
C.
イベント購読(Event Subscribers)
Event Subscribersは特殊なListenerで、前に述べたのは1つのlistenerに1つのhander()しか入れられません.イベントサブスクリプションは多くのプロセッサ(handler)を1つのクラスに入れて、1つのlistnerでそれらを集めることができます.このように異なるイベントは1つのlistnerに対応すればいいです.
php
namespace App\Listeners;
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 Illuminate\Events\Dispatcher $events
* @return array
*/
public function subscribe($events)
{
$events->listen(
'App\Events\UserLoggedIn',
'App\Listeners\UserEventListener@onUserLogin'
);
$events->listen(
'App\Events\UserLoggedOut',
'App\Listeners\UserEventListener@onUserLogout'
);
}
}
後ろのsubscribe()を見ると、各イベントとプロセッサが1つずつ対応しています.
Event SubscriberをService Providerにバインド
php
namespace App\Providers;
use Illuminate\Contracts\Events\Dispatcher as DispatcherContract;
use Illuminate\Foundation\Support\Providers\EventServiceProvider 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 = [
'App\Listeners\UserEventListener',
];
}
転載先:https://www.cnblogs.com/zuochuang/p/8963258.html