1. <?php
  2. namespace App\Controller;
  3.  
  4. class UsersController extends AppController
  5. {
  6. public function getMailer($name, Email $email = null)
  7. {
  8. if ($email === null) {
  9. $email = new Email();
  10. }
  11.  
  12. $className = App::className($name, 'Mailer', 'Mailer');
  13.  
  14. if (empty($className)) {
  15. throw new MissingMailerException(compact('name'));
  16. }
  17.  
  18. return (new $className($email));
  19. }
  20.  
  21. public function register()
  22. {
  23. $user = $this->Users->newEntity();
  24. if ($this->request->is('post')) {
  25. $user = $this->Users->patchEntity($user, $this->request->data());
  26. if ($this->Users->save($user)) {
  27. $this->getMailer('User')->send('welcome', [$user]);
  28. }
  29. }
  30. $this->set('user', $user);
  31. }
  32. }