1. Go to this page and download the library: Download drmmr763/laravel-asyncapi 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/ */
namespace App\AsyncApi;
use AsyncApi\Attributes\AsyncApi;
use AsyncApi\Attributes\Info;
use AsyncApi\Attributes\Server;
use AsyncApi\Attributes\Channel;
use AsyncApi\Attributes\Operation;
#[AsyncApi(
asyncapi: '3.0.0',
info: new Info(
title: 'My Application API',
version: '1.0.0',
description: 'AsyncAPI specification for my application'
),
servers: [
'production' => new Server(
host: 'api.example.com',
protocol: 'kafka',
description: 'Production Kafka server'
)
]
)]
class MyAsyncApiSpec
{
}
use Drmmr763\AsyncApi\Facades\AsyncApi;
// Build the specification
$spec = AsyncApi::build();
// Export to JSON
$json = AsyncApi::toJson();
// Export to YAML
$yaml = AsyncApi::toYaml();
// Export to file
AsyncApi::exportToFile('asyncapi.yaml', 'yaml');
AsyncApi::exportToFile('asyncapi.json', 'json');
// Scan for annotations
$annotations = AsyncApi::scan();
use Drmmr763\AsyncApi\AsyncApi;
class MyController
{
public function __construct(private AsyncApi $asyncApi)
{
}
public function generateSpec()
{
$specification = $this->asyncApi->build();
return response()->json($specification);
}
}
use AsyncApi\Attributes\Message;
use AsyncApi\Attributes\Schema;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
#[Message(
name: 'UserRegistered',
title: 'User Registered Event',
summary: 'Broadcast when a new user registers',
payload: new Schema(
type: 'object',
properties: [
'event' => new Schema(type: 'string', example: 'user.registered'),
'data' => new Schema(
type: 'object',
properties: [
'user' => new Schema(/* user schema */),
'ip_address' => new Schema(type: 'string')
]
)
]
)
)]
class UserRegistered implements ShouldBroadcast
{
// Your broadcast event implementation
}
// Define reusable schemas
class CommonSchemas
{
public const USER = '#/components/schemas/User';
public const ORDER = '#/components/schemas/Order';
public const PRODUCT = '#/components/schemas/Product';
}
// Reference them in your messages
#[AsyncApi(
channels: new Channels(
channels: [
'orders/created' => new Channel(
messages: [
'orderCreated' => new Message(
payload: new Schema(
type: 'object',
properties: [
'order' => new Reference(ref: CommonSchemas::ORDER),
'user' => new Reference(ref: CommonSchemas::USER)
]
)
)
]
)
]
),
components: new Components(
schemas: [
'User' => new Schema(/* user schema definition */),
'Order' => new Schema(
properties: [
'items' => new Schema(
type: 'array',
items: new Reference(ref: CommonSchemas::PRODUCT)
)
]
)
]
)
)]
class MyAsyncApiSpec {}
// app/AsyncApi/Schemas/UserSchemas.php
class UserSchemas
{
public const USER = '#/components/schemas/User';
public const USER_PROFILE = '#/components/schemas/UserProfile';
}
// app/AsyncApi/Schemas/OrderSchemas.php
class OrderSchemas
{
public const ORDER = '#/components/schemas/Order';
public const ORDER_ITEM = '#/components/schemas/OrderItem';
}
// app/AsyncApi/MainSpec.php
#[AsyncApi(
// Reference schemas from different classes
components: new Components(
schemas: [
'User' => new Schema(/* ... */),
'Order' => new Schema(/* ... */)
]
)
)]
class MainSpec {}