PHP code example of lumening / lumen-hprose

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

    

lumening / lumen-hprose example snippets


    'providers' => [
        // ...

       Lumening\LumenHprose\ServiceProvider::class,
    ]
    

    'aliases' => [
        // ...

         'LumenHproseRouter'=>Lumening\LumenHprose\Facades\Router::class,
    ]
    

       $app->register(Lumening\LumenHprose\ServiceProvider::class);
    

        $app->withFacades(true, [
            // ...
            'Lumening\LumenHprose\Facades\Router' => 'LumenHproseRouter',
        ]);
    

        protected $commands = [
        //...
        \Laravelista\LumenVendorPublish\VendorPublishCommand::class,
        ];
    

\LumenHproseRouter::add(string $name, string|callable $action, array $options = []);

\LumenHproseRouter::add('getUserByName', function ($name) {
    return 'name: ' . $name;
});

\LumenHproseRouter::add('userUpdate', 'App\Controllers\User@update', ['model' => \Hprose\ResultMode::Normal]);



namespace App\Controllers;

class User
{
    public function update($name)
    {
        return 'update name: ' . $name;
    }
    
    public function getUserById($id)
    {
        return [
            'id'=>$id,
            'name'=>'lumen',
            'email'=>'',
            'url'=>'http://www.lumen.fun',
        ];
    }
}

$client = new \Hprose\Socket\Client('tcp://127.0.0.1:8888', false);
$client->getUserByName('lumen');
$client->userUpdate('lumen');

\LumenHproseRouter::group(array $attributes, callable $callback);

\LumenHproseRouter::group(['namespace' => 'App\Controllers'], function ($route) {
    $route->add('getUserByName', function ($name) {
        return 'name: ' . $name;
    });
    
    $route->add('getUserById', 'User@getUserById');
    $route->add('userUpdate', 'User@update');
});

$client->getUserByName('lumen');
$client->userUpdate('lumen');

\LumenHproseRouter::group(['namespace' => 'App\Controllers', 'prefix' => 'user'], function ($route) {
    $route->add('getByName', function ($name) {
        return 'name: ' . $name;
    });

    $route->add('update', 'User@update');
});

$client->user->getByName('lumen');
$client->user->update('lumen');
// 或者
$client->user_getByName('lumen');
$client->user_update('lumen');

try{
    $client->user->getByName('lumen');
}catch(\Exception $e){
    $info = json_decode($e->getMessage(),true);
    $message = $info['message'];
    $code = $info['code'];
}
//更新tag

shell
    php artisan vendor:publish --provider="Lumening\LumenHprose\ServiceProvider"
    
shell
    php artisan vendor:publish --provider="Lumening\LumenHprose\ServiceProvider"
    

routes/rpc.php
shell
php artisan hprose:socket_server