PHPackages                             language/yii2-protobuf - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Parsing &amp; Serialization](/categories/parsing)
4. /
5. language/yii2-protobuf

ActiveLibrary[Parsing &amp; Serialization](/categories/parsing)

language/yii2-protobuf
======================

a wrapper to decode/encode protobuf

1.0.1-stable(7y ago)42091[2 issues](https://github.com/Languege/yii2-protobuf/issues)MITPHPPHP &gt;=7.0.0

Since Dec 1Pushed 7y ago1 watchersCompare

[ Source](https://github.com/Languege/yii2-protobuf)[ Packagist](https://packagist.org/packages/language/yii2-protobuf)[ RSS](/packages/language-yii2-protobuf/feed)WikiDiscussions master Synced today

READMEChangelogDependencies (2)Versions (3)Used By (0)

Yii Protobuf Extension is a wrapper for [php protobuf c-extension](https://github.com/protocolbuffers/protobuf/tree/master/php).It provides an easy way to decode/encoder protobuf data with Yii.In addition to，it provides a tool to generate php proto files from .proto.

You must install php [c-ext](https://github.com/protocolbuffers/protobuf/tree/master/php) before you can use this extension

### Requirements

[](#requirements)

To use PHP runtime library requires:

- C extension:protobuf &gt;= 3.5.0
- PHP package:php &gt;= 7.0.0
- Yii2.0 or above

### Installation

[](#installation)

You can install this extension by composer, as follows:

```
composer require language/yii2-protobuf
```

### Configure

[](#configure)

You need to add protobuf parser/formatter for request/response Component, as follows:

```
return [
    ...
    'components' => [
            ...
        'request' => [
            // !!! insert a secret key in the following (if it is empty) - this is required by cookie validation
            'cookieValidationKey' => 'inwyiHVV0KPon5UhGv6l0QYaWL4SC1ww',
            'parsers' => [
                'application/x-protobuf' => 'Language\Protobuf\ProtobufParser'
            ],
        ],
        'response' => [
            'formatters'=>[
                'protobuf' => [
                    'class'=>'Language\Protobuf\ProtobufResponseFormatter',
                    'hump'=>true, //By default, the field name is underlined to hump, for example, iphone_num is converted to IphoneNum.
                ],
            ],
        ],
        ...
    ],
]
```

As you can see, this extension use `application/x-protobuf` Content-Type to distinguish protobuf binary data.So, Client should set Content-Type as `application/x-protobuf` when it send protobuf binary data to Server

### Generate Proto

[](#generate-proto)

You can run build.sh shell script to generate proto files after Editing msg.proto. it will generate `PbApp` and `GPBMetadata`.You should always edit .proto instead of editing generated proto files

```
bash build.sh
```

### Register Proto

[](#register-proto)

You need to register .proto.php files for encode protobuf data after generate proto files.You can create a base controller and register them, As follows:

```
class BaseController extends Controller
{
    use ProtobufTrait;  //Inject using the trait attribute asProtobuf method

    public function init()
    {
        parent::init();
        // 消息文件注册
        ProtobufManager::register(ROOT . '/proto/msg.proto.php');
    }
}
```

ProtobufTrait provides `asProtobuf` method to convert php hash table to protobuf data

### Usage

[](#usage)

You should alway get request params with `$request->getBodyParams()`intead of `$_REQUEST`.ProtobufParser parser protobuf to array

```
