Native Browser Notifications
Notify users from inside PHP
Send real-time, native browser notifications to everyone via Symfony's Notifier Component.
composer require symfony/ux-notify
// ... use statements hidden - click to show
class NotifyController extends AbstractController
{
#[Route('/notify', name: 'app_notify')]
public function __invoke(UxPackageRepository $packageRepository, Request $request, ChatterInterface $chatter): Response
{
$package = $packageRepository->find('notify');
$form = $this->createForm(SendNotificationForm::class);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$message = SendNotificationForm::getTextChoices()[$form->getData()['message']];
// custom_mercure_chatter_transport is configured in config/packages/notifier.yaml
$message = (new ChatMessage(
$message,
new MercureOptions(['/demo/notifier'])
))->transport('custom_mercure_chatter_transport');
$chatter->send($message);
return $this->redirectToRoute('app_notify');
}
return $this->render('ux_packages/notify.html.twig', [
'package' => $package,
'form' => $form,
]);
}
}
{% extends 'base.html.twig' %}
{% block body %}
{{ stream_notifications(['/demo/notifier']) }}
{{ form_start(form) }}
{{ form_widget(form) }}
<button type="submit" class="btn btn-primary">Send Notification</button>
{{ form_end(form) }}
{% endblock %}
UX Notify
Install It
$ composer require symfony/ux-notify