
Yii2 Application Development Cookbook
By :

Yii's events provide a simple implementation, which allows you to listen and subscribe to various events that occur in your web-application. For example, you may wish to send a notification about a new article to followers each time you publish new material.
article
table:CREATE TABLE `article` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(255) DEFAULT NULL, `description` TEXT, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Article
model using Gii../yii serve
command.\controllers\SiteController
:public function actionTest() { $article = new Article(); $article->name = 'Valentine\'s Day\'s coming? Aw crap! I forgot to get a girlfriend again!'; $article->description = 'Bender is angry at Fry for dating a robot. Stay away from our women. You\'ve got metal fever, boy. Metal fever'; // $event is an object of yii\base\Event or a child class $article->on(ActiveRecord::EVENT_AFTER_INSERT, function($event) { $followers = ['[email protected]', '[email protected]', '[email protected]' ]; foreach($followers as $follower) { Yii::$app->mailer->compose() ->setFrom('[email protected]') ->setTo($follower) ->setSubject($event->sender->name) ->setTextBody($event->sender->description) ->send(); } echo 'Emails has been sent'; }); if (!$article->save()) { echo VarDumper::dumpAsString($article->getErrors()); }; }
config/web.php
component mailer
using the following code.'mailer' => [ 'class' => 'yii\swiftmailer\Mailer', 'useFileTransport' => false, ],
http://localhost:8080/index.php?r=site/test
.http://www.fakemailgenerator.com/inbox/teleworm.u
s/john2/
.We've created an Article
model and added a handler for the ActiveRecord::EVENT_AFTER_INSERT
event to our Article
model. It means that every time we save a new article an event is triggered and our attached handler will be called.
In the real-world, we would like to notify our blog followers each time we publish a new article. In a real application we would have a follower
or user
table and with different blog sections not only single blog. In this example, after saving our model we notify our followers [email protected]
, [email protected]
, and [email protected]
. In the last step we just prove that users have received our notifications, particularly john2
. You can create your own event with any name. In this example we use a built-in event called ActiveRecord::EVENT_AFTER_INSERT
, which is called after each insert to the database.
For example, we can create our own event. Just add a new actionTestNew
with the following code:
public function actionTestNew() { $article = new Article(); $article->name = 'Valentine\'s Day\'s coming? Aw crap! I forgot to get a girlfriend again!'; $article->description = 'Bender is angry at Fry for dating a robot. Stay away from our women. You've got metal fever, boy. Metal fever'; // $event is an object of yii\base\Event or a child class $article->on(Article::EVENT_OUR_CUSTOM_EVENT, function($event) { $followers = ['[email protected]', '[email protected]', '[email protected]' ]; foreach($followers as $follower) { Yii::$app->mailer->compose() ->setFrom('[email protected]') ->setTo($follower) ->setSubject($event->sender->name) ->setTextBody($event->sender->description) ->send(); } echo 'Emails have been sent'; }); if ($article->save()) { $article->trigger(Article::EVENT_OUR_CUSTOM_EVENT); } }
Also add the EVENT_OUR_CUSTOM_EVENT
constant to models/Article
as:
class Article extends \yii\db\ActiveRecord { CONST EVENT_OUR_CUSTOM_EVENT = 'eventOurCustomEvent'; … }
Run http://localhost:8080/index.php?r=site/test-new
.
You should see the same result and all notifications to followers will be sent again. The main difference is we used our custom event name.
After the save, we've triggered our event. Events may be triggered by calling the yii\base\Component::trigger()
method. The method requires an event name, and optionally an event object that describes the parameters to be passed to the event handlers.
For more information about events refer to http://www.yiiframework.com/doc-2.0/guide-concept-events.html
Change the font size
Change margin width
Change background colour