開源地址:https://github.com/flc1125/la...php
該項目支持 Laravel 及非 Laravel 項目使用,支持 php >= 5.3
版本語法laravel
注: 如下內容,默認你對 Hprose 已經有了必定了解。內容基於 Laravel 項目介紹。
環境依賴 C 擴展 hprose
,詳見:Hprose 官網、Hprose Pecl 擴展git
服務端安裝便可,客戶端可不安裝
pecl install hprose
安裝擴展包github
composer require flc/laravel-hprose
Laravel >= 5.5
已增長包自動發現。舊版本,請手動設置
在 config/app.php
文件下加入服務提供者web
'providers' => [ ... Flc\Laravel\Hprose\HproseServiceProvider::class, ]
在 config/app.php
文件下加入門面api
'aliases' => [ ... 'HproseRoute' => Flc\Laravel\Hprose\Facades\HproseRoute::class, 'HproseServer' => Flc\Laravel\Hprose\Facades\HproseServer::class, 'HproseClient' => Flc\Laravel\Hprose\Facades\HproseClient::class, ]
php artisan hprose:generator
執行完成後,會分別生成如下文件:服務器
app/Providers/HproseRouteServiceProvider.php
路由文件:routes/hprose.php
(該路由文件非 Laravel 路由,下文簡稱 Hprose 路由
)微信
Hprose 路由控制器根目錄默認在:app/Http/Controllers/Hprose
目錄下,可自行在app/Providers/HproseRouteServiceProvider.php
調整
在 config/app.php
文件下加入默認的 Hprose 路由服務提供者websocket
'providers' => [ ... app\Providers\HproseRouteServiceProvider::class, ]
php artisan vendor:publish --provider="Flc\Laravel\Hprose\HproseServiceProvider"
執行後,會生成配置文件路徑:/config/hprose.php
swoole
配置文件路徑:/config/hprose.php
<?php return [ 'server' => [ 'default' => 'http', 'connections' => [ 'http' => [ 'protocol' => 'http', ], ], ], 'client' => [ 'default' => 'http', 'connections' => array( 'http' => array( 'protocol' => 'http', 'uri' => 'http://192.168.2.67:9001/api/server', // 此處爲服務端的鏈接地址 'async' => false, ), ), ] ];
目前版本,除客戶端配置中的遠程服務器地址外,其餘配置默認便可
其餘均爲後續支持swoole-http
、tcp
、websocket
等,作提早架設
增長一個 Laravel 路由,用於啓動 Http 服務,如:
<?php Route::any('hprose-server', 'HproseController@server');
對應控制器:
<?php namespace App\Http\Controllers; use HproseServer; use HproseRoute; class HproseController { public function server() { HproseServer::setRouter(HproseRoute::getRouter())->start(); } }
訪問:http://localhost/hprose-server
<?php HproseRoute::add('tests', 'Controller@tests'); HproseRoute::add('tests_one', 'Controller@tests')->option(['...']);
Controller@tests
的方法自行定義
<?php namespace App\Http\Controllers; use HproseClient; class HproseController { public function client() { $result = HproseClient::tests('tests'); $result = HproseClient::connection('other')->tests('tests'); // 其餘鏈接 $result = HproseClient::connection()->tests->one('tests'); print_r($result); } }
服務端
<?php $router = new \Flc\Laravel\Hprose\Routing\Router; $router->group(['prefix' => 'tests', 'namespace' => 'App\\Controllers'], function ($router) { $router->add('one', 'Controller@one'); $router->add('two', 'Controller@two')->option(['...']); $router->group(['prefix' => 'group'], function ($router) { $router->add('one', 'Controller@group_one'); ... }); }); $app = [ 'config' => [ 'hprose.server.default' => 'http', 'hprose.server.connections' => [ 'http' => [ 'protocol' => 'http', ], ], ], ]; $server = new \Flc\Laravel\Hprose\Server($app); $server->setRouter($router)->start();
客戶端
<?php $app = [ 'config' => [ 'hprose.client.default' => 'http', 'hprose.client.connections' => [ 'http' => [ 'protocol' => 'http', 'uri' => 'http://localhost/server.php', 'async' => false ] ] ] ]; $client = new \Flc\Laravel\Hprose\Client($app); print_r($client->tests->one('222').PHP_EOL);
若是你以爲本擴展對你有幫助,請捐贈以表支持,謝謝~~
微信 | 支付寶 | ... |
MIT