Design Issue: Best place to put notifcation functions

Different areas of my app need to send notifications (emails), around 20 different scenarios (so these are spread across 3 controllers).

I want to make a model Notifications and put all the functions in there. The controllers can then load the model and fire off the relevant notifications.

Each function in the Notifications model, will load the relevant models needed to send to the appropriate message.

Is this a good design? Or should this be in a Controller component? (for CakePHP 3.x)

you forgot to tell cake version, for v. 3.1+ you have https://book.cakephp.org/3.0/en/core-libraries/email.html#creating-reusable-emails

1 Like

I tried the mailer. In my controller action:

namespace MyPlugin\Controller;
use Cake\Mailer\MailerAwareTrait;
use MyPlugin\Controller\AppController;

...
class QueryRepliesController extends AppController
{
    use MailerAwareTrait;

    public function add(){
        ...
        $this->getMailer('Notification')->send('newQueryReply', [$queryReply]);

In my Mailer:

namespace App\Mailer;

use Cake\Mailer\Mailer;

class NotificationMailer extends Mailer {
    
    public function newQueryReply($entity)
    {   

I get Mailer class “Notification” could not be found.

Help!

make sure your namespace are correct,
namespace MyPlugin\Controller; and namespace App\Mailer; are you sure they are correct? one is from plugin and second from your app?

also you didnt give your file names they also have to be correct

No both are in the myPlugin.

I’ve changed App/Mailer to MyPlugin/Mailer. Still Notification class not found.

if its in plugin you need to use plugin syntax - prefix everything you load from it with ‘MyPlugin.’ ie $this->getMailer('MyPlugin.Notifications');

1 Like

Doh! I see. Thanks. So silly.

dont worry cake is a lot to learn :slight_smile:

Thanks. Is there away to tell mailer not to use a template? I just want to send a short message, no need for view/template.

not when using Mailer, but there is https://book.cakephp.org/3.0/en/core-libraries/email.html#sending-messages-quickly for short one time messages

1 Like

Thanks. That’s useful. I’ve gone with a notification component instead.