click on Use This Template and create new Repo
then clone the repo
git clone https://github.com/{yourGithubUser}/laravel-microservices-redis.git
composer install
php artisan make:service {service_name}
Example :
php artisan make:service Product
This will create src\Services\ProductService and this folders will be created by default
src
├── Services
└── ProductService
├── DTO
│ └── ProductData.php
├── Enum
│ └── ProductEvent.php
├── Event
│ └── ProductCreatedEvent.php
│ └── ProductUpdatedEvent.php
│ └── ProductDeletedEvent.php
├── ProductRedisService.php
You can install the package via composer:
composer require elsayed85/lms-redis "@dev"
php artisan lms:install
You Must publish the config file with:
php artisan vendor:publish --tag="lms-redis-config"
This is the contents of the published config file:
<?php
return [
'service' => \Elsayed85\LmsRedis\LmsRedis::class,
'redis' => [
'client' => 'phpredis',
'options' => [
'cluster' => 'redis',
'prefix' => 'database_',
],
'default' => [
'url' => null,
'host' => '127.0.0.1',
'username' => null,
'password' => null,
'port' => '6379',
'database' => '0',
],
'cache' => [
'url' => null,
'host' => '127.0.0.1',
'username' => null,
'password' => null,
'port' => '6379',
'database' => '1',
],
],
];
Replace the service with the project redis service class (Created For You) [Example : ProductRedisService::class]
Also You Must Publish The Consume Command If You want To Handel The Incoming Redis Stream Events
php artisan vendor:publish --tag="lms-redis-consume-command"
NOTE : You need to schedule function in App\Console\Kernel.php
protected function schedule(Schedule $schedule)
{
$schedule->command('lms:consume')->everyMinute();
}
<?php
namespace app\Services;
use Elsayed85\LmsRedis\Services\ProductService as BaseRedisService;
use Elsayed85\LmsRedis\Services\ProductService\Event\ProductCreatedEvent;
use Elsayed85\LmsRedis\Services\ProductService\DTO\ProductData;
class ProductService extends BaseRedisService
{
public function publishProductCreated(ProductData $data): void
{
$this->publish(new ProductCreatedEvent($data));
}
}
<?php
namespace App\Actions;
use App\Models\Category;
use App\Models\Product;
use App\Services\ProductService;
class CreateProductAction
{
public function __construct(private readonly ProductService $redis)
{
}
public function execute(string $name, string $description, float $price): Product
{
$product = Product::create([
'name' => $name,
'description' => $description,
'price' => $price
]);
$this->redis->publishProductCreated(
$product->toData(),
);
return $product;
}
}
use Elsayed85\LmsRedis\Services\ProductService\DTO\ProductData;
class Product extends Model
{
use HasFactory;
public function toData(): ProductData
{
return new ProductData(
id : $this->id,
name : $this->name,
description : $this->description,
price : $this->price,
);
}
}
<?php
namespace App\Http\Controllers;
use App\Actions\CreateProductAction;
use App\Http\Requests\StoreProductRequest;
class ProductController extends Controller
{
public function store(StoreProductRequest $request, CreateProductAction $createProduct)
{
$product = $createProduct->execute(
$request->getName(),
$request->getDescription(),
$request->getPrice()
);
return response([
'data' => $product->toData()
], Response::HTTP_CREATED);
}
}
<?php
use App\Http\Controllers\ProductController;
Route::post('/v1/products', [ProductController::class, 'store']);
And That's It!
composer test
Please see CHANGELOG for more information on what has changed recently.
Please see CONTRIBUTING for details.
Please review our security policy on how to report security vulnerabilities.
The MIT License (MIT). Please see License File for more information.