Toggle Navigation
首页
Golang
PHP
Laravel
Magento
Linux
数据库
MySQL
Redis
Mongodb
热门知识
程序与生活
架构师之路
静态站点
GitHub
主页
PHP框架laravel中使用protobuf
PHP框架laravel中使用protobuf
08-02
Mark
507
之前我们分享了Google Protobuf的相关介绍以及基本数据类型,本篇文章就简单介绍下如何在php框架laravel中使用protobuf。 #### 1、安装配置php的protobuf环境: 最新版的protobuf已经支持PHP,所以只需在[https://github.com/google/protobuf/releases](https://github.com/google/protobuf/releases "https://github.com/google/protobuf/releases")这里安装protobuf即可,如下图所示:  本篇文章是基于windows下的php开发环境测试的,这里就选择windows下的protobuf安装程序。 #### 2、在laravel中建立相应的目录结构,然后执行命令解析proto生成对应的文件: 执行命令如下: ``` protoc --php_out="protobuf/compile" "protobuf/protos/comment.proto" ``` 生成后的目录截图如下:  #### 3、在laravel控制器中编写测试代码: 执行composer require "google/protobuf",安装laravel中解析protobuf的相关库。 在composer.json文件中配置如下: ``` "autoload": { "psr-4": { "App\\": "app/", "Protos\\": "protobuf/compile/Protos", "GPBMetadata\\Protobuf\\Protos\\": "protobuf/compile/GPBMetadata/Protobuf/Protos", } } ``` 控制器文件中代码如下: ```php public function testSendProtobuf(){ $from = new Comment(); $from->setId(1); $from->setText('Hello World!'); $from->setCreatedAt(date('Y-m-d h:i:s', time())); $packed = $from->serializeToString(); $client = new Client(); $r = $client->request('POST', 'http://127.0.0.1/protobuf/testReceiveProtobuf', [ 'body' => $packed ]); \Log::info($r->getBody()); } public function testReceiveProtobuf(Request $request){ $parsedComment= new Comment(); $parsedComment->mergeFromString($request->getContent()); return ['id' => $parsedComment->getId(), 'text' => $parsedComment->getText(), 'created_at' => $parsedComment->getCreatedAt()]; } ``` 配置好相关的路由,然后在浏览器中执行即可查看返回结果。 comment.proto文件内容如下: ``` syntax = "proto3"; package protos; message Comment { int32 id = 1; string text = 2; string created_at = 3; } ```
最新文章
Golang内存问题处理
WebSocket详解
Gopher面试中的Coding(三)
Golang用os/signal包实现平滑重载配置文件
Nginx实现负载均衡的几种方式
Golang中子goroutine与主协程同步的四种实现
Dockerfile文件详解
Golang开发微信公众号
最热文章
Magento实现下载csv数据表格
1324
Magento上传文件、缩略图和导出CSV
1238
Laravel带有条件搜索的分页
1148
Magento优化 – MySQL读写分离
1101
Magento后台添加rule规则
1052
Magento常用模块分享
870
Magento 建立自己的shell命令
812
10个实用的PHP正则表达式
799
热门标签
下载
上传
导出
laravel分页
读写分离
rule
规则
模块
shell
正则
Collection
binlog
命名空间
微信公众号
Git
解锁
锁表
string
处理
composer
事务处理
读写
架构
系统
表分割
函数
redis sentinel
time
日志分析
进阶
事务处理
缓存
package
schedule
交叉编译
HTTPS
fmt
golang
channel
事务
分布式锁
日志过滤
日志分割
mongodb
git服务器
gogs
多数据库
Dockerfile
json
php
protobuf
版本号
concat
发展方向
Gopher面试
get
post
goroutine
负载均衡
重载配置
模型关系
rpc
算法
mongodb
laravel
docker
elasticsearch
写文件
golang算法
defer
panic
redis
代码部署
主从配置
Dataflow
curl
package
Shell
生成插件
直接下载
并发
量级
数据类型
protobuf
参数
错误传递
队列
Go指针
成长
方法
websocket
grpc
路上
风景
protobuf
Redis持久化
yum
内存泄漏