PHP code example of vandarpay / laravel-grpc

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

    

vandarpay / laravel-grpc example snippets


use Spiral\RoadRunner\GRPC\ContextInterface;
use GrpcServices\Echo\Messages\PingMessage;

public function Ping(ContextInterface $ctx, PingMessage $in): PingMessage;

$this->bindGrpc(PingRepository::class, PingService::class);



namespace App\Protobuf\Clients;

use GrpcServices\Echo\Messages\PingMessage;
use GrpcServices\Notification\Messages\TextMessage;
use vandarpay\LaravelGrpc\GrpcClient;

class NotificationClient extends GrpcClient
{
    protected string $service = 'services.Notification';

    public function send(string $message): TextMessage
    {
        $request = new TextMessage();
        $request->setMsg($message . ' In Client ' . rand(0, 9999));

        return $this->client->simpleRequest('send', $request);
    }
}
bash
php artisan vendor:publish --provider=" vandarpay\LaravelGrpc\LaravelGrpcServiceProvider"
bash
php artisan make:service {service-name} --grpc --language=fa

syntax = "proto3";

package services;

option php_namespace = "GrpcServices\\Echo\\Messages";
option php_metadata_namespace = "GrpcServices\\Echo";

service Echo {
    rpc Ping (PingMessage) returns (PingMessage) {
    }
}

message PingMessage {
    string msg = 1;
}

package services;

option php_namespace = "GrpcServices\\Echo\\Messages";
option php_metadata_namespace = "GrpcServices\\Echo";
bash
protoc --proto_path=app/Protobuf --php_out=./app/Protobuf echo.proto
bash
protoc --proto_path=app/Protobuf --php_out=./app/Protobuf notification.proto

├── Services
    ├── Test 
    |   ├── v1
    |   |   ├── TestException.php
    |   |   ├── TestRepository.php
    |   |   ├── TestTransformer.php
    |   |   └── TestService.php
    |   └── v2
    |       ├── TestException.php
    |       ├── TestRepository.php
    |       ├── TestTransformer.php
    |       └── TestService.php
    └── AlphaService