PHP code example of otas / microservice
1. Go to this page and download the library: Download otas/microservice 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/ */
otas / microservice example snippets
# Laravel 11+ structure: bootstrap/app.php
->withMiddleware(function (Middleware $middleware) {
$middleware->alias([
'micro-service' => \Otas\MicroService\Http\Middleware\MicroServiceMiddleware::class,
'micro-service-establish-connection' => \Otas\MicroService\Http\Middleware\MicroServiceEstablishConnectionMiddleware::class,
]);
})
# Kernel structure: app/Http/Kernel.php
protected $middlewareAliases = [
//
'micro-service' => \Otas\MicroService\Http\Middleware\MicroServiceMiddleware::class,
'micro-service-establish-connection' => \Otas\MicroService\Http\Middleware\MicroServiceEstablishConnectionMiddleware::class,
];
Route::group([
'middleware' => [
'micro-service',
]
], function () {
// routes communicates with other microservices goes here
});
// EX: $microserviceName = 'payment';
// EX: $uri = 'license-payments' || 'orders' ...etc;
// `$params` and `$data` should be ['key' => 'value'] pair array
// which represent query string in get requests or inputs in post requests
$response = MsHttp::get($microserviceName, $uri, $params = []);
$response = MsHttp::post($microserviceName, $uri, $data = []);
$response = MsHttp::put($microserviceName, $uri, $data = []);
$schedule->command('ms:renew-keys')->everyThirtyMinutes()->onOneServer();
bash
php artisan config:cache
bash
php artisan ms:establish
bash
php artisan vendor:publish --tag=micro_service_migrations
php artisan migrate