PHP code example of poplary / lumen-hprose
1. Go to this page and download the library: Download poplary/lumen-hprose 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/ */
poplary / lumen-hprose example snippets
$app->register(\Poplary\LumenHprose\ServiceProvider::class);
use Poplary\LumenHprose\Facades\Router;
Router::add(string $name, string|callable $action, array $options = []);
Router::add('getServiceName', 'Poplary\LumenHprose\Controllers\DemoController@getServiceName');
namespace Poplary\LumenHprose\Controllers;
/**
* Class DemoController.
*/
class DemoController
{
/**
* @return string
*/
public function getServiceName(): string
{
return config('hprose.service');
}
}
$client = new \Hprose\Socket\Client('tcp://127.0.0.1:8889', false);
echo $client->getServiceName();
Router::group(array $attributes, callable $callback);
Router::group(['namespace' => 'Poplary\LumenHprose\Controllers'], function ($route) {
$route->add('getServiceName', 'DemoController@getServiceName');
});
echo $client->getServiceName();
Router::group(['namespace' => 'Poplary\LumenHprose\Controllers', 'prefix' => 'demo'], function ($route) {
$route->add('getServiceName', 'DemoController@getServiceName');
});
echo $client->demo->getServiceName();
// 或者
echo $client->demo_getServiceName();
try{
$client->user->getServiceName();
}catch(\Exception $e){
$info = json_decode($e->getMessage(),true);
$message = $info['message'];
$code = $info['code'];
}
shell
cp ./vendor/poplary/lumen-hprose/config/hprose.php ./config/hprose.php
cp ./vendor/poplary/lumen-hprose/routes/hprose.php ./routes/hprose.php
routes/hprose.php
shell
php artisan hprose:server
shell
php artisan hprose:client:demo