I have a component to send SMS. I would like to trigger in user table when an user just registered. Can I do that? If I can do, how?
I cannot see on docs: https://book.cakephp.org/3.0/en/controllers/components.html
I have a component to send SMS. I would like to trigger in user table when an user just registered. Can I do that? If I can do, how?
I cannot see on docs: https://book.cakephp.org/3.0/en/controllers/components.html
You don't do that, components are services for the controller layer, and you shouldn't use them outside of that context (CakePHP 2.x internally did use components outside of that context, but AFAIK that practice ended with CakePHP 3)!
Instead for example make your model dispatch an event:
// eventManager() before CakePHP 3.4
$this->getEventManager()->dispatch(
new \Cake\Event\Event('Model.Users.afterRegister', $this, ['user' => $userEntity])
);
which your component can listen to, and react with sending an SMS accordingly:
$manager = \Cake\Event\EventManager::instance();
$manager->on('Model.Users.afterRegister', function (\Cake\Event\Event $event) {
// subject() and data() before CakePHP 3.4
$usersTable = $event->getSubject();
$userEntity = $event->getData('user');
// ... send SMS
});
There surely are further ways to decouple things, for example dependency injection, so you may want to generally take some time and make yourself familiar with the subject of decoupling.