https://github.com/elsayed85/food-redis-service
https://github.com/elsayed85/food-redis-service
Last synced: about 1 year ago
JSON representation
- Host: GitHub
- URL: https://github.com/elsayed85/food-redis-service
- Owner: elsayed85
- License: mit
- Created: 2024-11-08T21:54:58.000Z (over 1 year ago)
- Default Branch: master
- Last Pushed: 2025-02-03T18:16:00.000Z (over 1 year ago)
- Last Synced: 2025-04-13T23:09:16.960Z (about 1 year ago)
- Language: PHP
- Size: 76.2 KB
- Stars: 0
- Watchers: 1
- Forks: 1
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE.md
Awesome Lists containing this project
README
## Development
### clone the repo
click on Use This Template and create new Repo
then clone the repo
```bash
git clone https://github.com/{yourGithubUser}/laravel-microservices-redis.git
```
### install the dependencies
```bash
composer install
```
### Create New Service
```bash
php artisan make:service {service_name}
```
Example :
```bash
php artisan make:service Product
```
This will create src\Services\ProductService and this folders will be created by default
```bash
src
├── Services
└── ProductService
├── DTO
│ └── ProductData.php
├── Enum
│ └── ProductEvent.php
├── Event
│ └── ProductCreatedEvent.php
│ └── ProductUpdatedEvent.php
│ └── ProductDeletedEvent.php
├── ProductRedisService.php
```
# Installation On Each Service
You can install the package via composer:
```bash
composer require elsayed85/lms-redis "@dev"
```
## Fast Installation
```bash
php artisan lms:install
```
## Manual Installation
### Config File [Required]
You Must publish the config file with:
```bash
php artisan vendor:publish --tag="lms-redis-config"
```
This is the contents of the published config file:
```php
\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]
### Consume Command [Optional]
Also You Must Publish The Consume Command If You want To Handel The Incoming Redis Stream Events
```bash
php artisan vendor:publish --tag="lms-redis-consume-command"
```
NOTE : You need to schedule function in App\Console\Kernel.php
```php
protected function schedule(Schedule $schedule)
{
$schedule->command('lms:consume')->everyMinute();
}
```
## Usage
#### Extend The Service Class [Fast Installation Do that for you]
```php
publish(new ProductCreatedEvent($data));
}
}
```
### Creating Actions
```php
$name,
'description' => $description,
'price' => $price
]);
$this->redis->publishProductCreated(
$product->toData(),
);
return $product;
}
}
```
#### ADD toData() function to your model
```php
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,
);
}
}
```
### AddAction To Controller
```php
execute(
$request->getName(),
$request->getDescription(),
$request->getPrice()
);
return response([
'data' => $product->toData()
], Response::HTTP_CREATED);
}
}
```
### Add Api Endpoint To Routes
```php