Blog

Learn how to integrate Drupal with Laravel in 2023

Drupal
Modules
PHP
Laravel
RabbitMQ
learn how to integrate drupal with laravel in 2023

Laravel and Drupal are two popular PHP-based web frameworks used to build web applications. Both have their unique set of features and advantages, and sometimes developers may want to integrate them to leverage the benefits of both. In this article, we will discuss how to integrate Laravel with Drupal by using RabbitMQ and provide some code examples to help you get started.

Before we dive into the integration process, let’s first understand what Laravel, Drupal, and RabbitMQ are and what they bring to the table.

Laravel is a free, open-source PHP web framework used for building web applications following the model-view-controller (MVC) architectural pattern. It comes with several features such as routing, database migrations, blade templating engine, and an Eloquent ORM. Laravel provides a clean and elegant syntax that makes it easy to learn and use, making it a popular choice among developers.

On the other hand, Drupal is a free, open-source content management system (CMS) written in PHP. It is used for building dynamic websites, blogs, and other web applications. Drupal provides a powerful set of features such as content management, user management, taxonomy, and extensibility. It is a popular choice among developers for building complex web applications.

Finally, RabbitMQ is a message-queueing software used for asynchronous messaging between applications. It allows for decoupling the frontend and backend, making it easier to scale and maintain the application.

Now that we have an understanding of what Laravel, Drupal, and RabbitMQ are let’s dive into the integration process.

Integration process:

The integration process involves creating a Laravel application that sends messages to RabbitMQ, and a Drupal application that consumes messages from RabbitMQ. Laravel will send messages containing data, and Drupal will consume those messages and process the data.

Step 1: Install required packages

The first step in the integration process is to install the required packages for Laravel and Drupal.

For Laravel, we need to install the php-amqplib/php-amqplib package, which is a PHP client library for RabbitMQ. To install this package, run the following command in your terminal:

composer require php-amqplib/php-amqplib

For Drupal, we need to install the php-amqplib/rabbitmq-bundle package, which is a Symfony bundle that provides integration with RabbitMQ. To install this package, run the following command in your terminal:

composer require php-amqplib/rabbitmq-bundle

Step 2: Configure RabbitMQ

The next step is to configure RabbitMQ. You can install RabbitMQ locally or use a cloud-based RabbitMQ service such as CloudAMQP or RabbitMQ as a service.

Once you have RabbitMQ installed and running, you need to create a queue for the messages. You can create a queue using the RabbitMQ management interface or by using the RabbitMQ CLI tool.

Step 3: Create a Laravel producer

The next step is to create a Laravel producer that sends messages to RabbitMQ. We will create a command that sends a message to RabbitMQ every time it is executed.

First, create a new command by running the following command in your terminal:

php artisan make:command SendMessage

In the SendMessage.php file, add the following code:

<?php

namespace App\Console\Commands;

use PhpAmqpLib\Connection\AMQPStreamConnection;
use PhpAmqpLib\Message\AMQPMessage;
use Illuminate\Console\Command;

class SendMessage extends Command
{
    protected $signature = 'send:message';
    protected $description = 'Send a message to RabbitMQ';

    public function handle()
    {
        $connection = new AMQPStreamConnection('localhost', 5672, 'guest', 'guest');
        $channel = $connection->channel();

        $channel->queue_declare('hello', false, false, false, false);

        $messageBody = [
            'message' => 'Hello, RabbitMQ!'
        ];
        $message = new AMQPMessage(json_encode($messageBody));

        $channel->basic_publish($message, '', 'hello');

        $this->info('Message sent to RabbitMQ');
        $channel->close();
        $connection->close();
    }
}

In the handle method, we create a new AMQPStreamConnection object, which connects to the RabbitMQ server. We then create a channel using the connection object and declare a queue named hello. Next, we create a message and publish it to the hello queue using the basic_publish method. Finally, we close the channel and connection objects.

Step 4: Create a Drupal consumer

The next step is to create a Drupal consumer that consumes messages from RabbitMQ. We will create a custom module that consumes messages from the hello queue and displays them on a page.

First, create a new module by creating a new directory called my_module in the modules/custom directory of your Drupal installation. Next, create a my_module.info.yml file in the my_module directory with the following contents:

name: My Module
type: module
description: 'My custom module'
core: 8.x

Next, create a my_module.routing.yml file in the my_module directory with the following contents:

my_module.hello:
  path: '/my-module/hello'
  defaults:
    _controller: '\Drupal\my_module\Controller\MyModuleController::hello'
    _title: 'Hello, RabbitMQ!'
  requirements:
    _permission: 'access content'

This defines a route that will be used to display the messages consumed from RabbitMQ.

Next, create a MyModuleController.php file in the my_module/src/Controller directory with the following contents:

<?php

namespace Drupal\my_module\Controller;

use PhpAmqpLib\Connection\AMQPStreamConnection;
use PhpAmqpLib\Message\AMQPMessage;
use Drupal\Core\Controller\ControllerBase;

class MyModuleController extends ControllerBase
{
    public function hello()
    {
        $connection = new AMQPStreamConnection('localhost', 5672, 'guest', 'guest');
        $channel = $connection->channel();

        $channel->queue_declare('hello', false, false, false, false);

        $messages = [];

        $callback = function ($message) use (&$messages) {
            $messageBody = json_decode($message->body, true);
            $messages[] = $messageBody['message'];
        };

        $channel->basic_consume('hello', '', false, true, false, false, $callback);

        while (count($channel->callbacks)) {
            $channel->wait();
        }

        $channel->close();
        $connection->close();

        return [
            '#theme' => 'item_list',
            '#items' => $messages,
        ];
    }
}

In the hello method, we create a new AMQPStreamConnection object, which connects to the RabbitMQ server. We then create a channel using the connection object and declare a queue named hello. Next, we create an empty array to hold the messages consumed from RabbitMQ.

We then create a callback function that is called whenever a message is consumed from the hello queue. The callback function decodes the message body and appends the message to the $messages array.

Next, we use the basic_consume method to start consuming messages from the hello queue. We use a while loop to wait for messages and process them until there are no more messages left to consume.

Finally, we close the channel and connection objects and return an array containing the messages consumed from RabbitMQ. We use the item_list theme to display the messages on a page.

Step 5: Test the integration

To test the integration, first start the Laravel producer by running the following command in your Laravel project directory:

php artisan send:message

This should send a message to the hello queue in RabbitMQ.

Next, visit the Drupal site and navigate to the /my-module/hello page. You should see the message sent by the Laravel producer displayed on the page.

Congratulations! You have successfully integrated Laravel with Drupal using RabbitMQ.