PHP code example of binhtv / grpc-base-laravel

1. Go to this page and download the library: Download binhtv/grpc-base-laravel 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/ */

    

binhtv / grpc-base-laravel example snippets




namespace App\Services\MicroserviceGrpc;

use binhtv\GrpcLaravel\Client\Contracts\BaseGrpcApi;
use Google\Protobuf\Internal\Message;
use Illuminate\Http\Request;
use Protobuf\Company\ExampleServiceClient;

/**
 * @method Message ExampleMethod(array|Request $request)
 */

class ExampleGrpcClient extends BaseGrpcApi
{
    public function grpcClient(): string
    {
        return ExampleServiceClient::class;
    }
}


 ...
// ExampleMethod is a method of ExampleServiceClient;
 (new ExampleGrpcClient())->ExampleMethod($request);
 ...

$clientGrpc = (new GrpcFactory)->make(ExampleServiceClient::class);

 ...
 use binhtv\GrpcLaravel\Client\Traits\HandleDataRequest;
 ...
 class ExampleController extends Controller
 {
    use HandleDataRequest;
 }



declare(strict_types=1);

use App\Grpc\ExampleGrpcController;
use Spiral\Goridge\StreamRelay;
use Spiral\RoadRunner\Worker;

ini_set('display_errors', 'stderr');

;

$app->singleton(
    binhtv\GrpcLaravel\Server\Contracts\ServiceInvoker::class,
    binhtv\GrpcLaravel\Server\LaravelServiceInvoker::class
);

$kernel = $app->make(binhtv\GrpcLaravel\Server\Kernel::class);

$kernel->registerService(ExampleGrpcController::class);

$w = new Worker(new StreamRelay(STDIN, STDOUT));

$kernel->serve($w);

shell
php artisan vendor:publish --tag=binhtv-grpc-config