Метод send класса Notifier
Метод send класса Notifier отправляет уведомление
получателю. Первым параметром передаётся объект
уведомления Notification, вторым - получатель
Recipient. Метод возвращает объект
SentMessage с информацией об отправке.
Синтаксис
public function send(Notification $notification, Recipient $recipient): SentMessage
Пример
Давайте отправим простое уведомление через notifier:
<?php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Notifier\Notification\Notification;
use Symfony\Component\Notifier\NotifierInterface;
use Symfony\Component\Notifier\Recipient\Recipient;
use Symfony\Component\Routing\Attribute\Route;
class NotifyController extends AbstractController
{
#[Route('/notify', name: 'app_notify')]
public function notify(NotifierInterface $notifier): Response
{
$notification = new Notification('hello');
$recipient = new Recipient('user@example.com');
$notifier->send($notification, $recipient);
return new Response('sent');
}
}
?>
Результат выполнения кода:
sent
Пример
Давайте отправим уведомление с темой и важностью:
<?php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Notifier\Notification\Notification;
use Symfony\Component\Notifier\NotifierInterface;
use Symfony\Component\Notifier\Recipient\Recipient;
use Symfony\Component\Routing\Attribute\Route;
class NotifyController extends AbstractController
{
#[Route('/notify-high', name: 'app_notify_high')]
public function notifyHigh(NotifierInterface $notifier): Response
{
$notification = new Notification('article');
$notification->subject('abcde');
$notification->importance('high');
$recipient = new Recipient('user@example.com');
$notifier->send($notification, $recipient);
return new Response('high sent');
}
}
?>
Результат выполнения кода:
high sent
Пример
Давайте отправим уведомление с явным указанием канала:
<?php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Notifier\Notification\Notification;
use Symfony\Component\Notifier\NotifierInterface;
use Symfony\Component\Notifier\Recipient\Recipient;
use Symfony\Component\Routing\Attribute\Route;
class NotifyController extends AbstractController
{
#[Route('/notify-channel', name: 'app_notify_channel')]
public function notifyChannel(NotifierInterface $notifier): Response
{
$notification = new Notification('abcde');
$notification->channels(['email']);
$recipient = new Recipient('user@example.com');
$notifier->send($notification, $recipient);
return new Response('channel sent');
}
}
?>
Результат выполнения кода:
channel sent
Смотрите также
-
класс
Notification,
который представляет объект уведомления -
класс
Notifier,
который управляет отправкой уведомлений -
класс
Recipient,
который представляет получателя уведомления -
метод
channels,
который задаёт каналы отправки уведомления