Design Issue: Best place to put notifcation functions


#1

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)


#2

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


#4

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!


#5

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


#6

No both are in the myPlugin.

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


#7

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


#8

Doh! I see. Thanks. So silly.


#9

dont worry cake is a lot to learn :slight_smile:


#10

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.


#11

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


#12

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