{"id":1285,"date":"2023-01-06T11:54:22","date_gmt":"2023-01-06T11:54:22","guid":{"rendered":"https:\/\/editjournal.redakt.eu\/faxmodem\/?p=1285"},"modified":"2023-03-01T08:55:03","modified_gmt":"2023-03-01T08:55:03","slug":"plugin-event-listener-location","status":"publish","type":"post","link":"https:\/\/editjournal.redakt.eu\/faxmodem\/blog\/development\/cakephp\/plugin-event-listener-location\/","title":{"rendered":"Where to put an event listener in a plugin?"},"content":{"rendered":"<p>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?<\/p>\n<p><a href=\"https:\/\/stackoverflow.com\/questions\/36392739\/cakephp-3-controller-event-implementation-example\/36392914#36392914\">Here<\/a> it says:<\/p>\n<blockquote>\n<p>A good place for this might be <code>Plugin\/src\/Controller\/Event<\/code><\/p>\n<\/blockquote>\n<p>And @admad on CakeSF Slack <a href=\"https:\/\/cakesf.slack.com\/archives\/C053DPNGT\/p1673001574266739\">says<\/a> it can also be <code>Plugin\/src\/Event\/<\/code>.<\/p>\n<p>So the question now is: <code>Controller<\/code> or not?<\/p>\n<p>I would say it makes sense to put it under <code>Plugin\/src\/Controller\/Event<\/code> only if the <code>View<\/code> 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 <code>src\/Events<\/code> and not bloat the Controller folder unnecessarily.<\/p>\n<p>If you need your listener to have access to the database, don't make the convenience of <code>loadModel()<\/code>\/<code>fetchTable()<\/code> lean you into using the controller unnecessarily. Instead, you can <a href=\"https:\/\/stackoverflow.com\/questions\/66812283\/in-cakephp4-how-to-access-a-model-within-a-model\">use the <code>LocatorAwareTrait<\/code><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>You&#8217;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\/.&hellip; <a class=\"more-link\" href=\"https:\/\/editjournal.redakt.eu\/faxmodem\/blog\/development\/cakephp\/plugin-event-listener-location\/\">Continue reading <span class=\"screen-reader-text\">Where to put an event listener in a plugin?<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1047],"tags":[844,715,1050],"class_list":["post-1285","post","type-post","status-publish","format-standard","hentry","category-cakephp","tag-best-practices","tag-cakephp","tag-cakephp-plugin-development","entry"],"_links":{"self":[{"href":"https:\/\/editjournal.redakt.eu\/faxmodem\/wp-json\/wp\/v2\/posts\/1285","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/editjournal.redakt.eu\/faxmodem\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/editjournal.redakt.eu\/faxmodem\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/editjournal.redakt.eu\/faxmodem\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/editjournal.redakt.eu\/faxmodem\/wp-json\/wp\/v2\/comments?post=1285"}],"version-history":[{"count":1,"href":"https:\/\/editjournal.redakt.eu\/faxmodem\/wp-json\/wp\/v2\/posts\/1285\/revisions"}],"predecessor-version":[{"id":1287,"href":"https:\/\/editjournal.redakt.eu\/faxmodem\/wp-json\/wp\/v2\/posts\/1285\/revisions\/1287"}],"wp:attachment":[{"href":"https:\/\/editjournal.redakt.eu\/faxmodem\/wp-json\/wp\/v2\/media?parent=1285"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/editjournal.redakt.eu\/faxmodem\/wp-json\/wp\/v2\/categories?post=1285"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/editjournal.redakt.eu\/faxmodem\/wp-json\/wp\/v2\/tags?post=1285"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}