Laravel Extension Based on hprose/hprose-php: Introduction of laravel-hprosed

01-28-2024

What this article brings to you is the introduction of Laravel extension: laravel-hprosed based on hprose/hprose-php, which has certain reference value. Friends who need it can refer to it and hope to help you. Laravel extension based on hprose/hprose-php: laravel-hprose

Version requires Laravel>=5.3.

Install composer require "zhuqipeng/laravel-hprose: v1.0-alpha"

Or edit composer.json

"require": { "zhuqipeng/laravel-hprose": "v1.0-alpha" }

Configure the ServiceProvider and facade registered in config/app.php (Laravel 5.5 does not need manual registration).

'providers' => [ // ... Zhuqipeng\LaravelHprose\ServiceProvider::class, ]

'aliases' => [ // ... 'LaravelHproseMethodManage' => Zhuqipeng\LaravelHprose\Facades\HproseMethodManage::class, ]

Configure the. env file

Listening address list, string json format array

HPROSE_URIS=["tcp://0.0.0.0:1314"]

Whether to enable the demo method, if true, turn it on; if false, turn it off, and a remote calling method demo will be automatically released.

Client can call: $client->demo ()

HPROSE_DEMO=true // true or false

Create a configuration and routing file:

php artisan vendor:publish --provider="Zhuqipeng\LaravelHprose\ServiceProvider"

A new file hprose.php will be automatically generated in the config directory under the application root directory.

A new file rpc.php will be automatically generated in the routes directory under the application root directory.

The usage of using routing is similar to laravel routing, and the routing code based on dingo/api has been modified simply.

Routing file

routes/rpc.php

Add routing method

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

String $name The name of the method that can be called remotely by the client.

String|callable $action class method, format: AppControllersUser@update.

Array $options is an associative array, which contains some special settings for this service function. For details, please refer to the introduction link of hprose-php official documentation.

Publish remote call methods getUserByName and update.

\LaravelHproseRouter::add('getUserByName', function ($name) { return 'name: ' . $name; }); \LaravelHproseRouter::add('userUpdate', 'App\Controllers\User@update', ['model' => \Hprose\ResultMode::Normal]);

controller

Copyright Description:No reproduction without permission。

Knowledge sharing community for developers。

Let more developers benefit from it。

Help developers share knowledge through the Internet。

Follow us