Team-Tea-Time / laravel-forum

A slim, lean forum package designed for quick and easy integration in Laravel projects
https://laravel-forum.teamteatime.net/
MIT License
601 stars 165 forks source link

How correctly use Event Listeners? #247

Closed NDanilov2015 closed 3 years ago

NDanilov2015 commented 3 years ago

Good day!

  1. I try link my event handler (eg mail notification about new posts messages created for Admin) to typical event listener: UserCreatingPost UserCreatingThread

In my code app\providers\EventServiceProvider.php I wrote:

protected $listen = [ 'Riari\Forum\Frontend\Events\UserCreatingPost' => [ 'App\Listeners\UserCreatingPostListener', ], ];

where "App\Listeners\UserCreatingPostListener" - my listeners.

But my listeners not calling in reality whem User written new post. What reasons can be?

  1. Also i need to create an event "reply to post author" aka "UserPostGetReplyEvent" to notify User about new messages to it. How create it in Forum Frontend?

Thank you.

Riari commented 3 years ago

Hi,

The user events provided by the frontend are triggered on GET routes, not POST/PATCH routes. They're designed for cases where you might want to display what a user is doing (e.g. "Writing a post") or for analytical purposes. If you want to actually react to a post being created, you should instead use Eloquent model events (specifically, the created event on the Post model). I won't go into details about how to do that since it's a Laravel feature, not part of the package - all you need to refer to from the package is the Post model.

Note that this is different in version 5, where events are provided by the package for every CRUD/BREAD action.

Hope that helps :)

NDanilov2015 commented 3 years ago

But send message - always not GET, but POST method.

Riari commented 3 years ago

But send message - always not GET, but POST method.

Yes, that's why you need to use Eloquent events instead.

The package events only cover GET routes. Eloquent events cover model inserts/updates/deletes resulting from POST/PATCH routes.

NDanilov2015 commented 3 years ago

Eloquent Events like "PostCreated" not fire.

Riari commented 3 years ago

I can't offer Laravel support, sorry. Eloquent events should cover your needs but they are outside the scope of this package.

NDanilov2015 commented 3 years ago

okay