Sharing technology, ideas, insights!
Call: +91 710 466 0336         Email: hello@sanisoft.com

Blog

Events in CakePHP 3 – A 4 step HowTo

CakePHP has a built in Events system since version 2.1. Events in CakePHP 3 will change Mark Story had written almost a year ago. The CakePHP Event system follows the Observer Pattern pretty closely. I am assuming that you are somewhat familiar with the CakePHP event system as it was in version 2.x if you are not and would like to know more then head over to this excellent post by Martin Bean.

In this post I will walk through a very simple scenario where you would dispatch an event when a Post is saved. A listener class listens for this event and writes to a log file whenever that event happens. Agreed this is not doing much but I would like to say that this just a proof of concept example to get you started with Events in CAkePHP 3. In reality the listeners will mostly do a lot more. Combine Events with something like Gearman and you can have really powerful scalable distributed application going.

Step 1

Install CakePHP 3 and Bake MVC for Posts. Does not really matter what your table for Post looks like as long as there is an id field which we will be using in the listener we create.

Step 2

Open the file PostsTable.php found in src/Model/Table and paste in the following afterSave method

The code is almost plain English. It says If a post is created then instantiate a new Event called ‘Model.Post.created’ and dispatch it using the eventManager! You can call your event whatever you want but it is good to follow a convention of naming them with “Layer.Object.verb”

Step 3

Next we create a listener class. The listener class is an implementation of Cake\Event\EventListener interface. The Listener classes implementing this interface needs to provide the implementedEvents() method. This method must return an associative array with all event names that the class will handle. I like to save my listeners in a folder called Event at the same level as Model, Controller etc. I namespaced it to App\Event so that they can be auto loaded via PSR-4. Create a file called PostListener.php in the Event folder and place the following code in it.

The implementedEvents tells the system which method to call when a given Event occurs. In our case updatePostLog method will be called when the event Model.Post.created. The updatePostLog method simply writes a message to the logs/debug.log file.

Step 4

Lastly we have to register this listener. For this we will use the globally available EventManager. Place the following code at the end of your config/bootstrap.php

That is it! Now go ahead create a post or two and you will messages similar to the following getting written in the debug.log

Now that you have a basic Event system going it would be a great idea to read the friendly manual and learn what more you can do with it – http://book.cakephp.org/3.0/en/core-libraries/events.html

About the Author

Dr. Tarique Sani is a pediatrician and forensic expert by education. He is a PHP programmer of ‘wrote the book’ caliber and has to his credit several very popular open source as well as commercial PHP projects.

He leads a team of dynamic programmers at SANIsoft who have in-depth understanding of Web scalability, development tools and usability practices with strong developmental skills in PHP, MySQL/PostgreSQL, HTML, Javascript, and Linux/Apache

2 comments

  1. Great post, it’s been a lot useful to me to create my first Event. I needed to change something in step 3 anyway: EventListener seems not to exist anymore, so you have to use EventListenerInterface:

    use Cake\Event\EventListenerInterface;
    class RequestListener implements EventListenerInterface

    Plus (this is my fault anyway) I struggled a bit because I was missing something else somewhere, and it turned out that I needed to add this to PostsTable.php

    use Cake\Event\Event;

Leave a Reply