PHP code example of vadimon / kafka-router-for-laravel

1. Go to this page and download the library: Download vadimon/kafka-router-for-laravel library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

vadimon / kafka-router-for-laravel example snippets


'providers' => [
    // Other Service Providers

    App\Providers\KafkaRouterServiceProvider::class,
],

KafkaRoute::dispatch($message);

KafkaRoute::dispatch($message, $connectionName);

KafkaRoute::topic('FirstTopic', 'KafkaController@handleFirst']);
KafkaRoute::topic('SecondTopic', [KafkaController::class, 'handleSecond']);
KafkaRoute::topic('ThirdTopic', InvokableController::class);
KafkaRoute::topic('FourthTopic', function (\RdKafka\Message $message) {
    // handle message
});
KafkaRoute::topic('*', function () {
});

// All other routes
KafkaRoute::topic('*', [KafkaController::class, 'fallbackHandler']);

KafkaRoute::connection('FirstConnection', base_path('routes/kafka/first.php'));
KafkaRoute::connection('SecondConnection', function () {
    KafkaRoute::topic('FirstTopic', 'KafkaController@handleFirstTopicOfSecondConnection']);
    KafkaRoute::topic('*', 'KafkaController@handleAnyTopicOfSecondConnection']);
});
KafkaRoute::topic('ThirdTopic', 'KafkaController@handleThirdTopicOfAnyConnection']);

// Middleware, defined as class
KafkaRoute::middleware(KaffkaMiddleware::class, function () {
    KafkaRoute::topic('*', 'KafkaController@handleMessage']);
});
// Middleware, defined as closure
$middleware = function (\RdKafka\Message $message, \Closure $next) {
    // some action
    $next($message);
}
KafkaRoute::middleware($middleware, function () {
    KafkaRoute::topic('*', 'KafkaController@handleMessage']);
});
// Multiple middlewares at once
KafkaRoute::middleware([Middleware1::class, Middleware2::class], function () {
    KafkaRoute::topic('*', 'KafkaController@handleMessage']);
});

KafkaRoute::group(['connection'=> 'SecondConnection', 'middleware' => KafkaMiddleware::class], function () {
    KafkaRoute::topic('*', 'KafkaController@handleMessage']);
});



namespace App\Kafka\Controllers;

use Illuminate\Routing\Controller;
use Vadimon\Laravel\Kafka\Router\Facades\KafkaRoute;
use Vadimon\Laravel\Kafka\Router\Router;

class KafkaController extends Controller
{
    public function handleFirstTopic(\RdKafka\Message $message, Router $router)
    {
        $currentRoute = $router->getCurrentRoute();
        $connectionName = $router->getCurrentConnectionName();
        // handle $message
    }

    public function handleSecondTopic()
    {
        $message = KafkaRoute::getCurrentMessage();
        $currentRoute = KafkaRoute::getCurrentRoute();
        $connectionName = KafkaRoute::getCurrentConnectionName();
        // handle $message
    }
}

function (\RdKafka\Message $message, \Closure $next) {
    // some action before routing to controller
    $next($message);
    // some action after routing to controller
}


 
namespace App\Kafka\Middleware;
 
class KafkaMiddleware
{
    public function handle(\RdKafka\Message $request, \Closure $next)
    {
        // some action before routing to controller
        $next($request);
        // some action after routing to controller
    }
}

class KafkaController extends Controller
{
    public function __construct()
    {
        $this->middleware(CommonMiddleware::class);
        $this->middleware(FirstTopicMiddleware::class)->only('handleFirstTopic');
        $this->middleware(OtherTopicsMiddleware::class)->except('handleFirstTopic');
    }
    
    public function handleFirstTopic(\RdKafka\Message $message)
    {
        // handle $message
    }
}
shell
php artisan vendor:publish --provider="Vadimon\\Laravel\\Kafka\\Router\\KafkaRouterPackageServiceProvider"