douliu8327 2018-03-21 12:57
浏览 12
已采纳

如何使用表对象中的组件? [关闭]

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

  • 写回答

1条回答 默认 最新

  • drnxnbf199049 2018-03-21 13:19
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 yolov8边框坐标
  • ¥15 matlab中使用gurobi时报错
  • ¥15 WPF 大屏看板表格背景图片设置
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真