Home  >  Article  >  Backend Development  >  PHP implements open source Axon Framework

PHP implements open source Axon Framework

WBOY
WBOYOriginal
2023-06-18 15:56:161002browse

Axon Framework is a lightweight CQRS (Command Query Responsibility Segregation) framework written in Java, which helps developers quickly implement microservice applications. As a PHP developer, you might be wondering how to use Axon Framework in PHP. This article will introduce you to the implementation details of PHP Axon Framework.

Advantages of Axon Framework

The core concept of Axon Framework is CQRS. It enables developers to break down applications into two main aspects: commands and queries. These two aspects are processed and stored in different models respectively. This allows for better scalability and performance. In addition, Axon Framework also provides a set of useful tools that make it easier for developers to implement event-driven applications.

Implementing the PHP version of Axon Framework

Axon Framework is written in JAVA, otherwise we can use it directly in PHP. Considering the popularity and ease of use of PHP, we can look at how to implement an Axon Framework in PHP.

  1. Install the necessary packages

To use PHP Axon Framework, we first need to install PHP.

Axon Framework relies on many third-party libraries, so we need to install Composer so that the dependencies required by Axon Framework can be automatically installed.

  1. Creating a PHP Axon Framework application

To create an Axon Framework application, we need to create a command controller (command bus) and a query controller (query bus ). Here we will use Symfony framework.

In order to use Axon Framework, we need to install the Axon bridge extension. To install this extension, use the following command:

$ composer require 1stformations/php-axon-bridge

Now we need to create a CLI console command for the application to issue commands using the Axon command bus. The command bus will be responsible for the delivery and processing of commands.

#!/usr/bin/env php

use SymfonyComponentConsoleApplication;
use SymfonyComponentConsoleInputInputArgument;
use SymfonyComponentConsoleInputInputInterface;
use SymfonyComponentConsoleInputInputOption;
use SymfonyComponentConsoleOutputOutputInterface;
use AxonSerializerSerializer;
use AxonCommandBusSimpleCommandBus;
use AxonFrameworkBridgePhpAxonBridge;

$autoloader = require __DIR__.'/vendor/autoload.php';

$app = new Application('Axon Framework Example', '0.0.1');

$app->register('places:create')
    ->addArgument('name', InputArgument::REQUIRED, 'Place name')
    ->addArgument('latitude', InputArgument::REQUIRED, 'Place latitude')
    ->addArgument('longitude', InputArgument::REQUIRED, 'Place longitude')
    ->setCode(function (InputInterface $input, OutputInterface $output) use ($autoloader) {
        $serializer = new SerializerJmsSerializer();
        $command = new PlaceCreatePlaceCommand(
            PlacesId::generate(),
            $input->getArgument('name'),
            $input->getArgument('latitude'),
            $input->getArgument('longitude')
        );
        $commandBus = new SimpleCommandBus();
        $axon = new PhpAxonBridge($commandBus, $serializer);

        $axon->sendCommand($command);
        
        $output->writeln('Place created');
});

$app->run();

In this example, we registered a console command places:create and wrote its code. This code uses the PHP Axon Bridge to publish Place/CreatePlaceCommand commands to the Axon command bus. The command controller will handle commands here.

We can send the query to the Axon Query Bus and receive the response using:

use AxonQueryBusQueryBus;
use AxonQueryHandlingSimpleQueryBus;

$queryBus = new SimpleQueryBus();
$axon = new PhpAxonBridge($commandBus, $serializer);
$query = new PlacesFindPlaceQuery($id);

$result = $axon->sendQuery(new QueryMessage($query));

Run PHP Axon Framework Application

Now we can use the following command to run PHP Axon application:

$ php example.php places:create --latitude=40.71 --longitude=-74.00 --name='New York City'

The console will output the Place created message. The Axon command bus will create a new Place instance using our command object, which is serialized to JSON and stored on disk. The query controller will use the "query object" to query the JSON file and return the query response.

Conclusion

This article introduces how to implement Axon Framework in PHP. We used the Symfony framework and the PHP Axon Bridge extension. Using Axon Framework, you can break your applications into smaller modules for better scalability and performance, and it's easier than ever. If you are a PHP developer and are considering implementing a microservices application, Axon Framework should be your first choice.

The above is the detailed content of PHP implements open source Axon Framework. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn