PHP code example of phunq / grpc-base-laravel

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

    

phunq / grpc-base-laravel example snippets




namespace App\Services\MicroserviceGrpc;

use phunq\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 phunq\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(
    phunq\GrpcLaravel\Server\Contracts\ServiceInvoker::class,
    phunq\GrpcLaravel\Server\LaravelServiceInvoker::class
);

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

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

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

$kernel->serve($w);

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