D
D
dev4002016-11-14 09:57:18
Yii
dev400, 2016-11-14 09:57:18

What is the best way to connect your functionality in yii2?

I want to make a Notifications class that will have 2 public methods, SMS notification, and email notification (wrapper for Yii::$app->mailer), and pull this class when registering, requesting a password, changing user personal data, and so on. What is the best way to connect it to the project? Make a namespace 'services', put this class there, and add it to web.php?

'components' => [
       ...
        'Notifications' => [
          'class' => 'app\services\Notifications',
        ],
       ...

Or is there any better way to do this? So far, the question of connecting my classes to the project is open for me, and the option to connect via web.php is taken from Google (because I'm just learning yii2). I'm using the basic template.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Fedorov, 2016-11-14
@dev400

Yes You can create a class, attach it to application components and use it everywhere in the application. But from the point of view of architecture, this is not camelfo, because. such an implementation creates unnecessary dependencies, with all the ensuing disadvantages. It is much better to implement this component on the event system embedded in Yii. Those. roughly speaking, you register in the system, at this moment a corresponding event is generated, and your Notifications class subscribes to this event and does the necessary work

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question