{"id":1045,"date":"2022-02-01T16:21:58","date_gmt":"2022-02-01T16:21:58","guid":{"rendered":"https:\/\/editjournal.redakt.eu\/faxmodem\/?p=1045"},"modified":"2023-03-01T18:22:15","modified_gmt":"2023-03-01T18:22:15","slug":"cakephp-events-getting-started-example","status":"publish","type":"post","link":"https:\/\/editjournal.redakt.eu\/faxmodem\/blog\/development\/cakephp\/cakephp-events-getting-started-example\/","title":{"rendered":"The very basics of getting started with CakePHP events"},"content":{"rendered":"<p>This is supposed to clarify what's already in the official documentation at <a href=\"https:\/\/book.cakephp.org\/3\/en\/core-libraries\/events.html\">https:\/\/book.cakephp.org\/3\/en\/core-libraries\/events.html<\/a><\/p>\n<ol>\n<li>\n<p>Create a listener. Can be a separate arbitrary class, or an existing class in the MVC scheme of things. (<a href=\"\/blog\/development\/where-to-put-an-event-listener-in-a-plugin\/\">Deciding what would be a good place for an event listener?<\/a>) Either way, it has to implement <code>\\Cake\\Event\\EventListenerInterface<\/code>. Lets say it was a standalone class and it went to <code>src\/Event\/ExampleListener.php<\/code><\/p>\n<\/li>\n<li>\n<p>Make this listener class have <code>implementedEvents()<\/code> method that looks like:<\/p>\n<pre><code>public function implementedEvents()\n{\n    return array(\n        'Model.afterSave' => 'exampleAfterSave',\n    );\n}<\/code><\/pre>\n<p>Hooking to <code>Model.afterSave<\/code> will trigger <code>exampleAfterSave<\/code> on every model throughout your application that has this listener attached. See below on how to filter.<\/p>\n<\/li>\n<li>\n<p>Add <code>exampleAfterSave()<\/code>:<\/p>\n<pre><code>public function exampleAfterSave($event, $entity, $options)\n{\n    \/\/ Skip if triggered not on the model we need\n    if ($event->getSubject()->getAlias() !== 'Users') {\n        return true;\n    }\n    \/\/ Do something\u2026\n}<\/code><\/pre>\n<\/li>\n<li>\n<p>Finally, the model that you would like to listen to needs to ne aware of that. In your e.g. <code>src\/Model\/Table\/UsersTable.php<\/code>, attach the listener to the <code>initialize()<\/code> method:<\/p>\n<pre><code>public function initialize(array $config)\n{\n    \/\/\u2026\n    $listener = new \\App\\ExampleCustomClasses\\ExampleListener();\n    $this->getEventManager()->on($listener);\n}<\/code><\/pre>\n<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>A quick example on how to get started with CakePHP event system.<\/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,852],"class_list":["post-1045","post","type-post","status-publish","format-standard","hentry","category-cakephp","tag-best-practices","tag-cakephp","tag-decoupling","entry"],"_links":{"self":[{"href":"https:\/\/editjournal.redakt.eu\/faxmodem\/wp-json\/wp\/v2\/posts\/1045","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=1045"}],"version-history":[{"count":7,"href":"https:\/\/editjournal.redakt.eu\/faxmodem\/wp-json\/wp\/v2\/posts\/1045\/revisions"}],"predecessor-version":[{"id":1347,"href":"https:\/\/editjournal.redakt.eu\/faxmodem\/wp-json\/wp\/v2\/posts\/1045\/revisions\/1347"}],"wp:attachment":[{"href":"https:\/\/editjournal.redakt.eu\/faxmodem\/wp-json\/wp\/v2\/media?parent=1045"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/editjournal.redakt.eu\/faxmodem\/wp-json\/wp\/v2\/categories?post=1045"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/editjournal.redakt.eu\/faxmodem\/wp-json\/wp\/v2\/tags?post=1045"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}