Laravel 5.7 gRPC Integration

Viewed 7965

I have setup gRPC in core PHP and it is working fine but I want to use it with laravel 5.7. I have downloaded gRPC using composer.json. Now I don't know what I have to do for using gRPC in it.

I have added below in my composer.json file

"require": {
        "datto/protobuf-php": "dev-master",
        "grpc/grpc": "^1.15.0"
    },
"repositories": [
        {
            "type": "vcs",
            "url": "https://github.com/stanley-cheung/Protobuf-PHP"
        }
    ]

But now I am not getting what should be next to do. So anyone knows then please let me know. I have my core PHP code for gRPC(core library example) which I have setup from this web url. gRPC in PHP. So I just want to integrate into laravel.

2 Answers

How to use protoc generated files with Laravel (5.7)

  • Prepare your .proto file describing your services as you would normally do
  • Set as package name package app.grpc;
  • Run the generation command: protoc --php_out=./ --grpc_out=./ --plugin=protoc-gen-grpc=/your/path/to/grpc_php_plugin yourfile.proto
  • Now copy App/Grpc inside laravel App
  • Copy GPBMetadata folder as new folder in laravel root
  • Change composer.json file inserting GPBMetadata location in the autoload classmap section (see below)
  • Run command composer dump-autoload
  • Now you can use your generated classes in any controller, remember to add use App\Grpc\YourClassName at the top.

let me know if someone have better solutions to this

Relevant part of composer.json:

...
    "autoload": {
        "classmap": [
            "database/seeds",
            "database/factories",
            "GPBMetadata"
        ],
        "psr-4": {
            "App\\": "app/"
        }
    }
...

you don't need these anymore

"datto/protobuf-php": "dev-master",

"repositories": [
        {
            "type": "vcs",
            "url": "https://github.com/stanley-cheung/Protobuf-PHP"
        }
    ]

Instead, you should add "google/protobuf": "^3.6.1" to your require.

See this as an example:

https://github.com/grpc/grpc/blob/master/examples/php/composer.json

The versions were out-of-date - just take the latest releases.

Related