Basic Usage of FOSMessageBundle
September 7, 2016 ยท View on GitHub
Basic operations involving FOSMessageBundle can be seen in the
Controller/MessageController file.
Get user threads
Get the threads in the inbox of the authenticated user::
$provider = $container->get('fos_message.provider');
$threads = $provider->getInboxThreads();
And the threads in the sentbox::
$threads = $provider->getSentThreads();
To get a single thread, check it belongs to the authenticated user and mark it as read::
$thread = $provider->getThread($threadId);
Manipulate threads
See FOS\\MessageBundle\\Model\\ThreadInterface for the complete list of available methods::
// Print the thread subject
echo $thread->getSubject();
// Get the tread participants
$participants = $thread->getParticipants();
// Know if this participant has read this thread
if ($thread->isReadByParticipant($participant))
// Know if this participant has deleted this thread
if ($thread->isDeletedByParticipant($participant))
Be careful when manipulating thread participants: you may have to persist the thread to synchronize
the thread metadata using $container->get('fos_message.thread_manager')->saveThread($thread, false)
(the second argument as false meaning to not flush the modifications).
Manipulate messages
See FOS\\MessageBundle\\Model\\MessageInterface for the complete list of available methods::
// Print the message body
echo $message->getBody();
// Get the message sender participant
$sender = $message->getSender();
// Get the message thread
$thread = $message->getThread();
// Know if this participant has read this message
if ($message->isReadByParticipant($participant))
Compose a message
Create a new message thread::
$composer = $container->get('fos_message.composer');
$message = $composer->newThread()
->setSender($jack)
->addRecipient($clyde)
->setSubject('Hi there')
->setBody('This is a test message')
->getMessage();
And to reply to this thread::
$message = $composer->reply($thread)
->setSender($clyde)
->setBody('This is the answer to the test message')
->getMessage();
Note that when replying, we don't need to provide the subject nor the recipient. Because they are the attributes of the thread, which already exists.
Send a message
Nothing's easier than sending the message you've just composed::
$sender = $container->get('fos_message.sender');
$sender->send($message);
Number of Unread Messages
You can return the number of unread messages for the authenticated user with::
$provider = $container->get('fos_message.provider');
$provider->getNbUnreadMessages();
Will return an integer, the number of unread messages.