PHP code example of kroderdev / laravel-microservice-core

1. Go to this page and download the library: Download kroderdev/laravel-microservice-core 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/ */

    

kroderdev / laravel-microservice-core example snippets


'services' => [
    'registry' => [
        'gateway' => [
            'url' => env('API_GATEWAY_URL', 'http://gateway.local'),
            'timeout' => 5,
            'retries' => 2,
        ],
        'users' => [
            'url' => env('USERS_SERVICE_URL'),
            'timeout' => 10,
            'retries' => 3,
        ],
    ],
],

use Kroderdev\LaravelMicroserviceCore\Services\ServiceClient;
use Illuminate\Support\Facades\Http;

// Via HTTP macro (auto-attaches correlation ID)
$response = Http::service('users')->get('/api/users');
$response = Http::service('gateway')->post('/auth/login', $credentials);

// With a bearer token
$response = Http::serviceWithToken('users', $token)->get('/api/profile');

// Via ServiceClient (instance methods)
$client = ServiceClient::to('users');
$client->withToken($token);
$users = $client->get('/api/users');

use Kroderdev\LaravelMicroserviceCore\Http\Middleware\ValidateJwt;
use Kroderdev\LaravelMicroserviceCore\Http\Middleware\LoadAccess;

// Individual middleware
Route::middleware(['jwt.auth'])->group(function () {
    Route::get('/profile', fn () => 'ok');
});

// Convenience group (JWT validation + permission loading)
Route::middleware(['microservice.auth'])->group(function () {
    Route::get('/dashboard', fn () => view('dashboard'));
});

Route::middleware(['jwt.auth', 'load.access', 'role:admin'])->group(function () {
    Route::get('/admin', fn () => 'admin area');
});

Route::middleware(['jwt.auth', 'load.access', 'permission:posts.create'])->group(function () {
    Route::post('/posts', [PostController::class, 'store']);
});

Gate::allows('role:admin');
Gate::allows('permission:posts.create');

'health' => [
    'enabled' => true,
    'path' => '/health',
],
bash
php artisan vendor:publish --provider="Kroderdev\LaravelMicroserviceCore\Providers\MicroserviceServiceProvider"