VOOZH about

URL: https://deepwiki.com/hypervel/notifications/4.2-anonymous-notifications

⇱ Anonymous Notifications | hypervel/notifications | DeepWiki


Loading...
Menu

Anonymous Notifications

Overview

The AnonymousNotifiable class enables sending notifications to recipients without requiring a persisted database model. This mechanism allows routing notifications directly to specific endpoints (email addresses, webhook URLs, etc.) for ad-hoc delivery scenarios where recipient information is not stored in the application database.

Use Cases:

  • Sending to email addresses not associated with User models
  • One-time notifications to external webhook endpoints
  • Guest or non-registered recipient notifications

For model-based notifications, refer to page 4.1.

Sources: src/AnonymousNotifiable.php11

The AnonymousNotifiable Class

The AnonymousNotifiable class is defined at src/AnonymousNotifiable.php11-70 and implements the notifiable contract without requiring database persistence.

Class Structure


Sources: src/AnonymousNotifiable.php11-70

Properties and Methods

MemberTypeLocationDescription
$routesarraysrc/AnonymousNotifiable.php16Stores routing information indexed by channel name
route()methodsrc/AnonymousNotifiable.php23-32Configures routing for a specific channel, returns $this for method chaining
notify()methodsrc/AnonymousNotifiable.php37-42Dispatches notification through Dispatcher::send()
notifyNow()methodsrc/AnonymousNotifiable.php47-52Dispatches notification through Dispatcher::sendNow()
routeNotificationFor()methodsrc/AnonymousNotifiable.php57-60Returns routing information for specified channel driver
getKey()methodsrc/AnonymousNotifiable.php67-69Returns null (no primary key for anonymous notifiables)

Sources: src/AnonymousNotifiable.php16-69

Routing Configuration

The route() method configures channel-specific routing information. Each channel name maps to destination data in the $routes array.

Route Method Signature

src/AnonymousNotifiable.php23-32

public function route(string $channel, mixed $route): static

The method:

  1. Validates that $channel is not 'database' (throws InvalidArgumentException if attempted)
  2. Stores $route in $this->routes[$channel]
  3. Returns $this for fluent method chaining

Example Routing Configurations

ChannelRoute Data TypeExample Value
mailstring"user@example.com"
slackstring"https://hooks.slack.com/services/XXX/YYY/ZZZ"
broadcastarray|string["channel-name", "event-name"]

Sources: src/AnonymousNotifiable.php23-32

Dispatch Methods

notify() Method

The notify() method at src/AnonymousNotifiable.php37-42 dispatches the notification through the Dispatcher interface resolved from ApplicationContext. This method respects the notification's queue configuration if it implements ShouldQueue.

Call Flow:

notify() → ApplicationContext::getContainer() → get(Dispatcher::class) → send($this, $notification)

notifyNow() Method

The notifyNow() method at src/AnonymousNotifiable.php47-52 bypasses queue processing and sends the notification immediately.

Call Flow:

notifyNow() → ApplicationContext::getContainer() → get(Dispatcher::class) → sendNow($this, $notification)

Sources: src/AnonymousNotifiable.php37-52

Notification Dispatch Flow

Complete Sequence Diagram

The following diagram shows the complete dispatch flow from application code through the notification system:


Sources: src/AnonymousNotifiable.php23-32 src/AnonymousNotifiable.php37-42 src/AnonymousNotifiable.php57-60

Channel Routing Resolution

Each notification channel calls routeNotificationFor() to retrieve the destination for that channel. The method is defined at src/AnonymousNotifiable.php57-60

Method Implementation

public function routeNotificationFor(string $driver): mixed
{
 return $this->routes[$driver] ?? null;
}

The method performs a simple array lookup returning null if the channel is not configured.

Routing Resolution by Channel


Sources: src/AnonymousNotifiable.php57-60

Comparison: Anonymous vs Model-Based Notifiables

AspectAnonymousNotifiableModel-Based Notifiable (e.g., User)
StorageIn-memory onlyPersisted in database
Primary KeygetKey() returns nullgetKey() returns database ID
RoutingUses $routes arrayUses trait method or model properties
Database ChannelNot supported (throws exception)Supported
Use CaseAd-hoc, one-time notificationsPersistent user notifications
Configurationroute() method callsrouteNotificationFor() override or trait

Sources: src/AnonymousNotifiable.php16 src/AnonymousNotifiable.php25-27 src/AnonymousNotifiable.php67-69

Limitations and Restrictions

Database Channel Restriction

The route() method at src/AnonymousNotifiable.php25-27 explicitly rejects the 'database' channel:

if ($channel === 'database') {
 throw new InvalidArgumentException('The database channel does not support on-demand notifications.');
}

Rationale: Database channel requires persisted notifiable entities with primary keys for storage and retrieval. Anonymous notifiables have no database representation.

No Primary Key

The getKey() method at src/AnonymousNotifiable.php67-69 returns null. This prevents anonymous notifiables from being used with any functionality that requires entity identification or database lookups.

Sources: src/AnonymousNotifiable.php25-27 src/AnonymousNotifiable.php67-69

Usage Examples

Email to Non-User Address


Calls: src/AnonymousNotifiable.php23-32src/AnonymousNotifiable.php37-42

Slack Webhook Notification


Multi-Channel Anonymous Notification


Note: notifyNow() at src/AnonymousNotifiable.php47-52 bypasses queue processing.

Sources: src/AnonymousNotifiable.php23-32 src/AnonymousNotifiable.php37-52

Integration with the Notification System

Anonymous notifications integrate with the broader notification system through the Dispatcher interface and the container. The following diagram shows how they fit into the notification flow:


The AnonymousNotifiable class uses the application container to resolve the Dispatcher interface, which is implemented by the ChannelManager class.

Sources: src/AnonymousNotifiable.php37-52

Summary

Anonymous notifications provide a flexible way to send notifications to recipients who don't exist as models in your application. By using the AnonymousNotifiable class, you can route notifications to specific endpoints through various channels without the need for a persisted recipient model.