Installation
composer require div-looper/inbox-bundle
Add to config/bundles.php:
return [
// ...
DivLooper\InboxBundle\DivLooperInboxBundle::class => ['all' => true],
];
Database Migration Run migrations to create the required tables:
php bin/console doctrine:migrations:diff
php bin/console doctrine:migrations:migrate
First Use Case Create a message entity via the bundle’s service:
use DivLooper\InboxBundle\Entity\Message;
$message = new Message();
$message->setSender('user@example.com');
$message->setSubject('Hello!');
$message->setBody('This is a test message.');
$message->setRecipient($userEntity); // User entity from your app
$em = $this->getDoctrine()->getManager();
$em->persist($message);
$em->flush();
Viewing Messages Use the bundle’s controller to fetch messages for a user:
$messages = $this->get('div_looper_inbox.message_manager')->getMessagesForUser($user);
Programmatic Sending
Use the MessageManager service to send messages programmatically:
$manager = $this->get('div_looper_inbox.message_manager');
$manager->sendMessage($recipientUser, 'subject', 'body', $senderUser);
Event-Based Sending Trigger message creation via Symfony events (e.g., after user registration):
// In your event subscriber
$message = new Message();
$message->setSender($this->getUser());
$message->setSubject('Welcome!');
$message->setBody('Thanks for registering.');
$message->setRecipient($user);
$em->persist($message);
Fetch unread messages for a user:
$unreadMessages = $manager->getUnreadMessagesForUser($user);
Mark messages as read:
$manager->markMessagesAsRead($messages);
Middleware/Guard Integration
Attach the bundle’s logic to your authentication system (e.g., FOSUserBundle):
// In a controller or service
$user = $this->getUser();
$messages = $manager->getMessagesForUser($user);
Twig Integration Pass messages to Twig templates:
{% for message in messages %}
<div class="message">
<h3>{{ message.subject }}</h3>
<p>{{ message.body }}</p>
<small>From: {{ message.sender.email }}</small>
</div>
{% endfor %}
Extend the Message entity to add custom fields:
namespace App\Entity;
use DivLooper\InboxBundle\Entity\Message as BaseMessage;
class CustomMessage extends BaseMessage
{
private $customField;
// Getters/setters
}
Override the bundle’s services via configuration:
# config/packages/div_looper_inbox.yaml
div_looper_inbox:
message_class: App\Entity\CustomMessage
Database Schema Assumptions
User entity uses non-standard field names (e.g., email_address instead of email), override the Message entity or use a custom repository.Message entity and update the recipient/sender mappings:
/**
* @ORM\ManyToOne(targetEntity="App\Entity\User", inversedBy="messages")
* @ORM\JoinColumn(name="recipient_id", referencedColumnName="id")
*/
private $recipient;
Missing Event Listeners
use DivLooper\InboxBundle\Entity\Message;
use Doctrine\ORM\Event\LifecycleEventArgs;
public function postPersist(Message $message, LifecycleEventArgs $args)
{
// Log or dispatch an event
}
Permission Handling
if (!$this->get('security.authorization_checker')->isGranted('ROLE_USER')) {
throw $this->createAccessDeniedException();
}
Performance with Large Datasets
$paginator = $this->get('knp_paginator');
$messages = $paginator->paginate(
$manager->getMessagesForUser($user),
$pageNumber,
20 // items per page
);
Enable SQL Logging
Add this to config/packages/dev/doctrine.yaml to debug queries:
doctrine:
dbal:
logging: true
profiling: true
Check Entity Relationships
If messages aren’t saving/loading correctly, verify the recipient/sender relationships in the Message entity. Use:
php bin/console doctrine:schema:validate
Clear Cache After Customization
After extending the Message entity or overriding services, clear the cache:
php bin/console cache:clear
Custom Message Types
trait NotifiableMessage
{
public function isUrgent(): bool
{
return strpos($this->subject, 'URGENT') !== false;
}
}
Add Attachments
Extend the Message entity to support file attachments:
/**
* @ORM\OneToMany(targetEntity="App\Entity\MessageAttachment", mappedBy="message", cascade={"persist", "remove"})
*/
private $attachments;
Override Templates
The bundle likely uses Twig templates for emails/notifications. Override them in templates/DivLooperInboxBundle/:
{# templates/DivLooperInboxBundle/Messages/message.html.twig #}
Custom message template here.
API Integration Expose message endpoints via API Platform or FOSRestBundle:
# config/api_platform/resources.yaml
resources:
DivLooper\InboxBundle\Entity\Message:
collectionOperations:
get:
method: GET
path: /messages
security: "is_granted('ROLE_USER')"
How can I help you explore Laravel packages today?