event
์ปจํธ๋กค๋ฌ๊ฐ ์ด๋ฒคํธ๋ฅผ ๋ฑ๋กํ๋ฉด ์ด ์กํธ์ ๊ด์ฌ์ด ์๋ ํด๋์ค๊ฐ ์์ ์ด ํด์ผํ ์ผ์ ์คํํ๋ ๊ธฐ์ ๋ก ์ด๋ ์ปจํธ๋กค๋ฌ๋ ์์ ์ด ๋ฐ์์ํจ ์ด๋ฒคํธ๋ฅผ ๋๊ฐ ์ด๋ป๊ฒ ์ฌ์ฉํ๋์ง ์์ง ๋ชปํ๋ค. (๋์จํ ๊ฒฐํฉ)
์ฅ์
์ปจํธ๋กค๋ฌ ์ฝ๋ ๊ฐ๊ฒฐ
์ด๋ฒคํธ ์ฒ๋ฆฌ ๋ก์ง์ ์ ์ฐํ๊ฒ ํ์ํ๊ฑฐ๋ ์ถ์ํ ์ ์๋ค.
์ฌ์ฉ์์๊ฒ ๋น ๋ฅธ UI์๋ต์ ์ ๊ณตํ๋ค. (๋ฌด๊ฒ๊ณ ์๊ฐ์ด ์ค๋ ๊ฑธ๋ฆฌ๋ ์ผ์ ์ด๋ฒคํธ๋ก ๋์ ธ ๋ค๋ฅธ ํด๋์ค์์ ์ฒ๋ฆฌ)
Controller
class ArticlesController extends Controller{
public function store(\App\Http\Requests\ArticlesRequest $request){
$article = \App\User::find(1)->articles()->create($request->all());
if(!$article){
return back()->with('flash_message','๊ธ์ด ์ ์ฅ๋์ง ์์์ต๋๋ค.')->withInput();
}
var_dump('์ด๋ฒคํธ๋ฅผ ๋ฐ์');
event('article.created', [$article]);
var_dump('์ด๋ฒคํธ ๋ฐ์ ์๋ฃ');
return redirect(route('articles.index'))->with('flash_message','์์ฑํ์ ๊ธ์ด ์ ์ฅ๋์์ต๋๋ค.');
}
}
event()
๋ฉ์๋๋ฅผ ์ด์ฉํด์ ์ด๋ฒคํธ๋ฅผ fireํ ์ ์๊ณ ์ด๋ ๋ฉ์๋์ ์ฒซ๋ฒ์งธ ์ธ์๋ก ์ด๋ฒคํธ ์ด๋ฆ
์ ๋๋ฒ์งธ๋ก ์ด๋ฒคํธ ๋ฐ์ดํฐ
๋ฅผ ์ ์ํด์ฃผ๋ฉด ๋๋ค.
์ด๋ฒคํธ ์์
1. ๋ผ์ฐํฐ ์ด์ฉ
Event::listen('article.created', function ($aticle){
var_dump($article->toArray());
});
2. ์ด๋ฒคํธ ๋ ์ง์คํธ๋ฆฌ
class EventServiceProvider extends ServiceProvider{
protected $listen = [
Registered::class => [
SendEmailVerificationNotification::class,
],
];
public function boot()
{
parent::boot();
\Event::listen('article.created',function ($article){
var_dump($article->toArray());
});
}
}
๋ผ์ฐํ
์ ์ ํ์ผ์ ๋ชจ๋ ์ด๋ฒคํธ ์ฒ๋ฆฌ ๋ก์ง์ ๋ด๊ฒ ๋๋ฉด ๊ฐ๋
์ฑ์ด ๋จ์ด์ง๊ณ ๋ฌด๊ฑฐ์์ง๊ธฐ ๋๋ฌธ์ ์ด๋ฒคํธ๋ ์ ์ฉ์ผ๋ก ์ฒ๋ฆฌํ๋ ํด๋์ค๊ฐ ์กด์ฌํ๋๋ฐ ๋ฐ๋ก app/Providers/EventServiceProvider.php
์ด๋ค. ์์ ํ ์ด๋ฒคํธ์ ์ฒ๋ฆฌ ๋ก์ง์ boot()๋ฉ์๋์ ์์ฑํ๋ฉด ๋๋ค.
๋ฉ์๋ ๋ด์์ Eventํ์ฌ๋๋ฅผ ์ด์ฉํด์ ๋ก์ง์ ์ ์ํ๋ฉด ๋๋ค.
3. ์ด๋ฒคํธ ๋ฆฌ์ค๋ ํด๋์ค
class ArticlesEventListener
{
public function __construct()
{
//
}
public function handle(\App\Models\Article $article)
{
var_dump($article->toArray());
}
}
//EventServiceProvider
public function boot()
{
parent::boot();
\Event::listen('article.created',function ($article){
\App\Listeners\ArticlesEventListener::class
});
}
๋ผ์ฐํธ ์ ์ ํ์ผ์์ ์ฝ๋ฐฑํจ์๋ฅผ ์ปจํธ๋กค๋ฌ๋ก ์ฎ๊ฒผ๋ฏ์ด ์ด๋ฒคํธ ์ฒ๋ฆฌ๋ก์ง๋ ๋ณ๋์ ๋ฆฌ์ค๋๋ก ๋ถ๋ฆฌํ ์ ์๋๋ฐ php artisan make:listener ArticlesEventListener
๋ก ์์ฑํ๋ฉด App/Events/ ๋ฐ์ ๋ฆฌ์ค๋๊ฐ ์์ฑ์ด๋๊ณ Provider๋ ๋ฆฌ์ค๋ ํ์ฌ๋๋ฅผ ํธ์ถํ๋ฉด handle()๋ฉ์๋๊ฐ ๋์ํ์ฌ ์ด๋ฒคํธ๋ฅผ ๋์ํ๋ค.
์ด๋ฒคํธ ํด๋์ค
class ArticleCreated
{
use Dispatchable, InteractsWithSockets, SerializesModels;
public $article;
public function __construct(\App\Modles\Article $article)
{
$this->article = $article;
}
public function broadcastOn()
{
return new PrivateChannel('channel-name');
}
}
//Controller
public function store(\App\Http\Requests\ArticlesRequest $request){
event(new \App\Events\ArticleCreated($article));
}
//Provider
public function boot{
parent::boot();
\Event::listen(
\App\Evetns\ArticleCreated::class,
\App\Listeners\ArticlesEventListener::class
);
}
//Listener
class ArticlesEventListener
{
public function handle(\App\Events\ArticlesEvent $event)
{
var_dump($event->article->toArray());
}
}
์ด ์ด๋ฒคํธ ํด๋์ค๋ ์ ๋ฌํ ๋ฐ์ดํฐ๋ฅผ ๋ด๊ณ ์์ผ๋ฉฐ ์ผ์ข ์ DTO์ด๋ค. DTO์ด๊ธฐ ๋๋ฌธ์ ๋ค๋ฅธ ํด๋์ค๊ฐ ์ด ํ๋กํผํฐ์ ์ ๊ทผํ ์ ์๋๋ก public์ผ๋ก ์ ์ธํด์ฃผ๋ฉด ๋๋ค. ์ปจํธ๋กค๋ฌ์์ ์ด ์ ์ธํ Event์ธ์คํด์ค๋ฅผ ์์ฑํ์ฌ event()๋ฉ์๋๋ก ๋์ ธ์ฃผ๊ณ ์ด๋ฒคํธ ๋ ์ง์คํธ๋ฆฌ(Provider)์ ์๋ก๋ง๋ ์ด๋ฒคํธ ์ฑ๋์ ๋ฑ๋กํด์ฃผ๋ฉด ๋๋ค.
listen()๋ฉ์๋์ ์ฒซ๋ฒ์งธ ์ธ์๋ก ์ง์ ํ ์ด๋ฒคํธ๊ฐ ๋ฐ์ํ๋ฉด, ๋๋ฒ์งธ ์ธ์์ ํด๋์ค(listener)์๊ฒ ์ด๋ฒคํธ ์ฒ๋ฆฌ๋ฅผ ์์ํ๋ ํํ์ด๋ค. ์ด๋, Listener์ handle()๋ฉ์๋์ ์ธ์๋ Event์ฑ๋๋ก ๋ฐ๊ฟ์ฃผ์ด์ผ ํ๊ณ event๊ฐ์ฒด๋ด๋ถ์ ๋ชจ๋ธ์ด ๋ค์ด์๋ ๊ฒ์ด๊ธฐ ๋๋ฌธ์ ํ๋ฒ๋ ์ฒด์ด๋์ ๊ฑฐ์ณ ์ ๊ทผ์ ํด์ฃผ์ด์ผ ํ๋ค.
+๋ฒ์ธ)
tail -f storage/logs/laravel.log
๋ช
๋ น์ด๋ก ๋ผ๋ผ๋ฒจ ๋ก๊ทธ๋ฅผ ์ฝ๊ฒ ํ์ธํ ์ ์๋ค.
๋ผ๋ผ๋ฒจ ๋ด์ฅ ์ด๋ฒคํธ ์ฑ๋
์ด๋ฒคํธ๋ฅผ ๊ผญ ์ปจํธ๋กค๋ฌ
์์๋ง ๋์ ธ์ผ ํ๋ ๊ฒ์ ์๋๊ณ ์ด๋์๋ ๋์ง ์ ์์ผ๋ฉฐ, ๋ผ๋ผ๋ฒจ ํ๋ ์์ํฌ์์๋ ์ด๋ฒคํธ๋ฅผ fireํ๋๋ฐ ์ฌ์ฉ์ ์ธ์ฆ
,์บ์
,์ฝ์
,db
,๋ฉ์ผ
๋ฑ์ ๊ธฐ๋ฅ์์ ๋์ง๊ณ ์๋ค.
๋ง์ด๊ทธ๋ ์ด์
/๋ชจ๋ธ
class CreateAuthorsTable extends Migration
{
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->timestamp('last_login')->nullable();
});
}
public function down()
{
Schema::dropIfExists('users');
}
}
์๋กํํธ ๋ชจ๋ธ์ ์๊ฐ๊ณผ ๊ด๋ จ๋ ํ๋กํผํฐ๋ฅผ ์นด๋ณธ(Carbon\Carbon)
์ธ์คํด์ค๋ก ๋ฐํํ์ฌ App\Models\User::find(1)->created_at->diffForHumans();
์ ๊ฐ์ด ์ฌ์ฉํ๋ฉด ํ์ฌ ์๊ฐ์ผ๋ก๋ถํฐ ๋ช์๊ฐ ์ ์ธ์ง์ ๊ฐ์ ์๊ฐ ๊ณ์ฐ์ ํ์ํ ํธ๋ฆฌํ API๋ฅผ ์ ๊ณตํ๋ค. ์ด ์ธ์๋ Carbon ๋ฌธ์์์ ๋ง์ api๋ฅผ ํ์ธ ํ ์ ์๋ค.
Last updated