Where to put an event listener in a plugin?

in CakePHP


You're creating an event listener that inserts a database record every time the matching event fires. And you need the listener to be in a plugin. Which folder do you put it in?

Here it says:

A good place for this might be Plugin/src/Controller/Event

And @admad on CakeSF Slack says it can also be Plugin/src/Event/.

So the question now is: Controller or not?

I would say it makes sense to put it under Plugin/src/Controller/Event only if the View layer is involved. So if the listener is non-interactive and the only thing it does is silently write an entry into database with no output, it will be more transparent to put it under src/Events and not bloat the Controller folder unnecessarily.

If you need your listener to have access to the database, don't make the convenience of loadModel()/fetchTable() lean you into using the controller unnecessarily. Instead, you can use the LocatorAwareTrait

#cakephp #best-practices #cakephp-plugin-development