Answer the question
In order to leave comments, you need to log in
How to use RabbitMqBundle in Symfony4?
What I did:
- installed Symfony 4
- installed RabbitMQ
- checked sending to and from the queue as it is written in the manual on the RabbitMQ website. Everything is working.
Next
- put RabbitMqBundle in Symfony
Created such a setting in config/old_sound_rabbit_mq.yaml
old_sound_rabbit_mq:
connections:
default:
url: '%env(RABBITMQ_URL)%'
producers:
# use 'old_sound_rabbit_mq.task_producer' service to send data.
task:
connection: default
exchange_options: { name: 'task', type: direct }
<?php
namespace App\Controller;
use App\Entity\Setting;
use App\Form\SettingType;
use App\Repository\SettingRepository;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
/**
* @Route("/setting")
*/
class SettingController extends AbstractController
{
public function edit(Request $request, Setting $setting): Response
{
$form = $this->createForm(SettingType::class, $setting);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$this->getDoctrine()->getManager()->flush();
//////////// Send msg to queue ////////////
$msg = ['msg' => "Settings was changed!"];
$this->get('old_sound_rabbit_mq.task_producer')->publish(serialize($msg));
////////////////////////////////////////////////
return $this->redirectToRoute('setting_index');
}
return $this->render('setting/edit.html.twig', [
'setting' => $setting,
'form' => $form->createView(),
]);
}
}
Answer the question
In order to leave comments, you need to log in
Documentation
class TaskController
{
/**
* @var ProducerInterface
*/
private $producer;
public function __construct(ProducerInterface $producer)
{
$this->producer = $producer;
}
public function indexAction($name)
{
$this->producer->publish('test');
return new Response();
}
}
# services.yaml
services:
App\Controller\TaskController:
arguments:
- '@old_sound_rabbit_mq.task_producer'
# services.yaml
services:
OldSound\RabbitMqBundle\RabbitMq\ProducerInterface: '@old_sound_rabbit_mq.task_producer'
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question