PHP code example of fadhila36 / grpcavel

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

    

fadhila36 / grpcavel example snippets


namespace App\Grpc\Services;

use Grpcavel\Attributes\GrpcService;
use Grpcavel\Attributes\GrpcMethod;
use App\Grpc\Requests\GetUserRequest;
use App\Grpc\Responses\UserResponse;

#[GrpcService]
class UserService
{
    #[GrpcMethod]
    public function getUser(GetUserRequest $request): UserResponse
    {
        $user = \App\Models\User::findOrFail($request->id);
        
        return UserResponse::fromModel($user);
    }
}

class GetUserRequest extends GrpcRequest
{
    public function __construct(public readonly int $id) {}

    public function rules(): array
    {
        return ['id' => '

#[GrpcService]
#[Middleware(Authenticate::class)]
class SecureService {
    #[GrpcMethod]
    #[Middleware(LogRequest::class)]
    public function handle(...) {}
}

#[GrpcService]
#[Middleware(\Grpcavel\Middleware\RateLimitMiddleware::class)]
class PublicService { ... }

$response = GrpcClient::call(UserService::class, 'getUser', new GetUserRequest(id: 1));

expect($response)->toBeInstanceOf(UserResponse::class);
bash
php artisan grpc:install
bash
php artisan grpc:make-service UserService
bash
php artisan grpc:sync
bash
php artisan grpc:start
bash
php artisan grpc:make-client UserServiceClient
bash
php artisan grpc:cache
bash
php artisan grpc:clear