
This project aim to offer a simple chat service to any symfony application.
As this project is very new, we decided not to add a receipe on Symfony Flex at the moment so the configuration must be performed manually.
First install the dependencies by running this comand in your repository
composer require btba/chat-bundle
and register the bundle
// config/bundles.php
return [
//your bundles
Btba\ChatBundle\BtbaChatBundle::class => ['all' => true]
];
then in your app directory add a config file. The following parameters are mandatory:
# config/packages/btba_chat.yaml
btba_chat:
update_interval: 1000
message_class: App\Entity\ChatMessage
author_class: App\Entity\User
update_intervalrefers to the time between two refresh of the chat
message_classrefers to the ORM class that host the messages (Doctrine supported)
author_classrefers to the ORM class that host the auhors (Doctrine supported)
Then register the bundle routes and change the prefix according to your needs
# config/routes/btba_chat.yaml
btba_chat:
resource: '@BtbaChatBundle/Resources/config/routes.yaml'
prefix: /chat-bundle/
In order to save authors and messages in your database you need to create at least two classes that extends the bundle model as such:
// App\Entity\User
/**
* @ORM\Entity(repositoryClass="App\Repository\UserRepository")
*/
class User extends BaseAuthor implements UserInterface
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
protected $id;
/**
* @ORM\Column(type="string", length=180, unique=true)
*/
protected $username;
/**
* @ORM\OneToMany(targetEntity="App\Entity\ChatMessage", mappedBy="author", cascade={"persist"}, orphanRemoval=true)
*/
private $messages;
// App\Entity\ChatMesssage
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
use Btba\ChatBundle\Model\BaseChatMessage;
/**
* @ORM\Entity(repositoryClass="App\Repository\ChatMessageRepository")
*/
class ChatMessage extends BaseChatMessage
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
protected $id;
/**
* @ORM\Column(type="string", length=255)
*/
protected $content;
/**
* @ORM\Column(type="datetime")
*/
protected $date;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\User", inversedBy="messages", cascade={"persist"})
* @ORM\JoinColumn(nullable=false)
*/
protected $author;
}
and in the message_class repository add the following trait:
// src/Repository/ChatMessageRepository.php
namespace App\Repository;
use Btba\ChatBundle\Query\MessageQuery;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
class ChatMessageRepository extends ServiceEntityRepository
{
use MessageQuery;
//your code...
}
configuration is over you are good to go !
To use this bundle, you need to add several component to your views.
If you're using encore, add the following assets to you're app.css file
@import '../../vendor/btba/chat-bundle/assets/css/chat.css';
and app.js file
import * as chat from '../../vendor/btba/chat-bundle/assets/js/chat';
//functions for the chat window management
$(function(){
$("#chevron").click(function(e) {
chat.changeChevron(e.target);
});
$("#chat-submit").click(function(e) {
chat.submitChat(e);
});
});
in your view you now just have to render the following controller:
{{ render(controller('Btba\\ChatBundle\\Controller\\ChatController::show')) }}
How can I help you explore Laravel packages today?