codebuds/mattermost-publication-bundle
Install the Bundle
composer require codebuds/mattermost-publication-bundle
Enable the bundle in config/bundles.php:
return [
// ...
Codebuds\MattermostPublicationBundle\MattermostPublicationBundle::class => ['all' => true],
];
Configure the Bundle
Add mattermost_publication.yaml to config/packages/:
mattermost_publication:
webhook_url: '%env(MATTERMOST_WEBHOOK_URL)%'
username: 'LaravelApp'
channel: '#notifications'
icon_url: 'https://example.com/logo.png'
Set the environment variable in .env:
MATTERMOST_WEBHOOK_URL="https://your-mattermost-instance/hooks/xxxx"
First Use Case
Inject MattermostPublication into a controller or service and publish a message:
use Codebuds\MattermostPublicationBundle\Service\MattermostPublication;
public function notifyUser(MattermostPublication $publication) {
$publication->publish("Hello from Laravel!");
}
MattermostPublication directly into methods or constructors.
public function __construct(private MattermostPublication $publication) {}
public function onUserRegistered(UserRegisteredEvent $event) {
$this->publication->publish("New user: {$event->user->name}");
}
Override default config per message:
$publication->publish(
"Custom message",
[
'channel' => '#admin',
'username' => 'AdminBot',
]
);
For bulk operations (e.g., logging), use a loop with error handling:
foreach ($logs as $log) {
try {
$publication->publish($log->message, ['username' => 'LoggerBot']);
} catch (\Exception $e) {
Log::error("Mattermost publish failed: " . $e->getMessage());
}
}
Wrap publish() in a job for async processing:
// Job class
public function handle() {
$this->publication->publish("Async notification");
}
// Dispatch
NotifyMattermostJob::dispatch($publication);
Webhook URL Validation
MATTERMOST_WEBHOOK_URL in .env; invalid URLs cause silent failures.curl first:
curl -X POST -H 'Content-Type: application/json' -d '{"text":"test"}' $WEBHOOK_URL
Rate Limiting
sleep(1); // 1-second delay between messages
Icon URL Requirements
icon_url must be publicly accessible. Use absolute URLs (e.g., https://).Exception Handling
publish() calls in try-catch to avoid breaking user flows:
try {
$publication->publish("Critical alert!");
} catch (\Exception $e) {
// Log or notify admins
}
Enable Debug Mode
Add to config/packages/mattermost_publication.yaml:
debug: true
This logs raw payloads to Symfony’s debug toolbar.
Check Payload Structure
Use dd($publication->getPayload($message)) to inspect the final JSON before sending.
Custom Payloads Extend the service to support rich messages (e.g., buttons, attachments):
$publication->publishWithPayload([
'text' => 'Alert!',
'attachments' => [/* ... */],
]);
Multiple Webhooks
Register multiple configurations in config/packages/ (e.g., mattermost_dev.yaml, mattermost_prod.yaml) and switch via environment.
Middleware Add a middleware to auto-publish on specific routes:
public function handle($request, Closure $next) {
$response = $next($request);
$publication->publish("Route accessed: {$request->path()}");
return $response;
}
How can I help you explore Laravel packages today?