1. Go to this page and download the library: Download agencelex/notifications library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
agencelex / notifications example snippets
use Lex\Notifications\Domain\Model\Ability\Notifiable;
use Lex\Notifications\Domain\Model\Ability\HasRouteNotificationForMail;
class FrontendUser extends AbstractEntity
{
use Notifiable;
use HasRouteNotificationForMail; // Useful for email delivery (it brings the ?string { return 'Doe'; }
}
use Lex\Notifications\Notification;
use Lex\Notifications\NotificationChannel;
use Lex\Notifications\NotificationLevel;
use TYPO3\CMS\Core\Mail\MailMessage;
use Lex\Notifications\Slack\Message\SlackMessage;
use Lex\Notifications\MicrosoftTeams\Message\TeamsMessage;
use Lex\Notifications\MicrosoftTeams\Message\AdaptiveCard;
use Lex\Notifications\MicrosoftTeams\Message\Element\TextBlock;
use Lex\Notifications\MicrosoftTeams\Message\Action;
final class OrderConfirmed extends Notification
{
public function __construct(private readonly Order $order) {}
// Optional - If you want to specify a notification level different from INFO
public function getLevel(): int
{
return NotificationLevel::LEVEL_INFO;
}
// Specify delivery channels
public function via(object $notifiable): array
{
return [
NotificationChannel::CHANNEL_MAIL, // Because of this, toMail is return SlackMessage::text(':white_check_mark: *Job finished*');
}
public function toTeams(object $notifiable): TeamsMessage
{
return TeamsMessage::create()
->card(
AdaptiveCard::make()
->body([TextBlock::make('New order placed')->weight('Bolder')])
->actions([Action\OpenUrl::make('View', 'https://example.com')])
);
}
// Here you implement the method
// From the notifiable itself
$user->notify(new OrderConfirmed($order)); // queued (if ShouldQueue)
$user->notifyNow(new OrderConfirmed($order)); // immediate
// From any service via the dispatcher
$this->notificationDispatcher->send($user, new OrderConfirmed($order));
$this->notificationDispatcher->sendNow($user, new OrderConfirmed($order));
// Multiple recipients
$this->notificationDispatcher->send([$userA, $userB], new Announcement());
// To a specific channel, via() is ignored
$this->notificationDispatcher->channel(NotificationChannel::CHANNEL_DATABASE)->send($user, new InvoicePaid($invoice));
// To someone who is not a model (see "On-Demand Notifications")
$this->notificationManager->route(NotificationChannel::CHANNEL_MAIL, '[email protected]')
->route('slack', '#orders')
->notify(new OrderReceiptEmail($order));
// Frontend user → frontend user
$sender = $this->notifiableFrontendUserRepository->findByUid($senderUid);
$recipients = $this->notifiableFrontendUserRepository->findByUids($recipientUids);
$this->notificationDispatcher->send($recipients, new ContentSharedWithYou($page, $sender));
// Extension → backend user (plain class, no DB record needed)
$adminA = new class ($backendEmailA) {
use Notifiable;
use HasRouteNotificationForMail;
public function __construct(protected readonly string $email) {}
public function getEmail(): string { return $this->email; }
}
$adminA->notifyNow(new MaliciousAttempt($collectedData));
$adminB = new class($backendEmailB, $backendRealName) {
use Notifiable;
use HasRouteNotificationForMail;
protected string $firstName;
protected string $lastName;
public function __construct(protected readonly string $email, string $fullName) {
$parts = explode(' ',trim($fullName), 2);
$this->firstName = $parts[0] ?? '';
$this->lastName = $parts[1] ?? '';
}
public function getEmail(): string { return $this->email; }
public function getFirstName(): ?string { return $this->firstName ; }
public function getLastName(): ?string { return $this->lastName; }
};
$adminB->notifyN(new SchedulerJobFailed($error));
// Any code → inline email recipient
$contact = new class($data) {
use Notifiable;
public function __construct(protected array $data) {}
// For each custom channel this notifiable should support, implement a route to tell the system the recipient/target/enpoint. Ex: routeNotificationForSlack
};
$contact->notifyNow(new OrderReceiptEmail($order));
use Lex\Notifications\NotificationChannel;
use Lex\Notifications\NotificationDispatcherInterface;
use Symfony\Component\Mime\Address;
public function __construct(private readonly NotificationDispatcherInterface $notificationDispatcher) {}
// One channel
$this->notificationDispatcher
->route(NotificationChannel::CHANNEL_MAIL, '[email protected]')
->notify(new OrderReceiptEmail($order));
// Several channels: chain route() calls
$this->notificationDispatcher
->route(NotificationChannel::CHANNEL_MAIL, new Address('[email protected]', 'Support'))
->route('slack', '#orders')
->notify(new OrderReceived($order));
// Several channels at once: routes()
$this->notificationDispatcher
->routes([
NotificationChannel::CHANNEL_MAIL => '[email protected]',
'slack' => '#orders',
])
->notifyNow(new OrderReceived($order));
use Lex\Notifications\Channel\ChannelInterface;
use Symfony\Component\DependencyInjection\Attribute\AutoconfigureTag;
use Lex\Notifications\Notification;
#[AutoconfigureTag('notifications.channel')]
final class SlackChannel implements ChannelInterface
{
public function __construct(private readonly SlackClient $slack) {}
// Optional: provide a short string key used in via().
// If omitted, the short class name (SlackChannel) is used as the key.
public function getName(): string
{
return 'slack';
}
public function send(object $notifiable, Notification $notification): void
{
// Implement your logic to send the notification
$this->slack->post(
// Works for models (routeNotificationForSlack()) and on-demand recipients
$notifiable->routeNotificationFor('slack', $notification),
$notification->toSlack($notifiable)
);
}
}
public function via(object $notifiable): array
{
return ['slack', NotificationChannel::CHANNEL_DATABASE];
}
// Here you implement the method nForSlack(object $notifiable, Notification $notification): string
{
return 'https://hooks.slack.com/services/your/slack/webhook';
}
use Lex\Notifications\Channel\ChannelInterface;
use Symfony\Component\DependencyInjection\Attribute\AutoconfigureTag;
#[AutoconfigureTag('notifications.channel')]
final class TeamsChannel implements ChannelInterface
{
// ... Your channel logic
}
use Lex\Notifications\Channel\ChannelInterface;
use Symfony\Component\DependencyInjection\Attribute\Autoconfigure;
#[Autoconfigure(public: true, tags: ['notifications.channel'])]
final class TeamsChannel implements ChannelInterface
{
// ... Your channel logic
}
final class OrderConfirmed extends Notification implements ShouldQueue { }
// In a frontend plugin
$uid = $this->getContext()->getAspect('frontend.user')->get('id');
$notifications = $this->notificationRepository->findByNotifiable($uid);
// Assign the notifications to the view
$this->view->assign('notifications', $notifications);
// Later, mark as read or remove all
$this->notificationRepository->markAllAsReadForNotifiable($uid);
$this->notificationRepository->removeAllForNotifiable($uid);
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.