The content of this article is about the Laravel extension developed based on hprose/hprose-php: the introduction of laravel-hprosed, which has certain reference value. Friends in need can refer to it. I hope it will be helpful to you. help.
Laravel extension developed based on hprose/hprose-php: laravel-hprose
Laravel>=5.3
composer require "zhuqipeng/laravel-hprose:v1.0-alpha"
or edit composer. json
"require": { "zhuqipeng/laravel-hprose": "v1.0-alpha" }
Register ServiceProvider and Facade in config/app.php (Laravel 5.5 does not require manual registration)
'providers' => [ // ... Zhuqipeng\LaravelHprose\ServiceProvider::class, ]
'aliases' => [ // ... 'LaravelHproseMethodManage' => Zhuqipeng\LaravelHprose\Facades\HproseMethodManage::class, ]
Configuration.env file
List of listening addresses, string json format array
HPROSE_URIS=["tcp://0.0.0.0:1314"]
Whether to enable the demo method, true to enable, false to close, enable Afterwards, a remote calling method will be automatically released to the outside worlddemo
The client can call: $client->demo()
HPROSE_DEMO=true // true or false
CreateConfiguration
and Route
Files:
php artisan vendor:publish --provider="Zhuqipeng\LaravelHprose\ServiceProvider"
New files will be automatically generated in the config
directory under the application root directoryhprose.php
A new file will be automatically generated in the routes
directory under the application root directory rpc.php
The usage of routing is similar to laravel
. A simple modification has been made to the routing code based on dingo/api
Routing file
routes/rpc.php
Add routing method
\LaravelHproseRouter::add(string $name, string|callable $action, array $options = []);
string $name The method name 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 the service function. For details, please refer to the hprose-php official document introduction link
Publish remote call methodgetUserByName
and update
\LaravelHproseRouter::add('getUserByName', function ($name) { return 'name: ' . $name; }); \LaravelHproseRouter::add('userUpdate', 'App\Controllers\User@update', ['model' => \Hprose\ResultMode::Normal]);
Controller
<?php namespace App\Controllers; class User { public function update($name) { return 'update name: ' . $name; } }
Client call
$client->getUserByName('zhuqipeng'); $client->userUpdate('zhuqipeng');
Routing group
\LaravelHproseRouter::group(array $attributes, callable $callback);
array $attributes attributes ['namespace' => '', 'prefix' => '']
callable $callback callback function
\LaravelHproseRouter::group(['namespace' => 'App\Controllers'], function ($route) { $route->add('getUserByName', function ($name) { return 'name: ' . $name; }); $route->add('userUpdate', 'User@update'); });
Client call
$client->getUserByName('zhuqipeng'); $client->userUpdate('zhuqipeng');
Prefix
\LaravelHproseRouter::group(['namespace' => 'App\Controllers', 'prefix' => 'user'], function ($route) { $route->add('getByName', function ($name) { return 'name: ' . $name; }); $route->add('update', 'User@update'); });
Client call
$client->user->getByName('zhuqipeng'); $client->user->update('zhuqipeng'); // 或者 $client->user_getByName('zhuqipeng'); $client->user_update('zhuqipeng');
php artisan hprose:socket_server
Related recommendations:
Laravel framework routing configuration summary and setting tips, laravel framework
Laravel framework Extension functions, methods of extending custom classes, laravel framework
The above is the detailed content of Laravel extension developed based on hprose/hprose-php: Introduction to laravel-hprosed. For more information, please follow other related articles on the PHP Chinese website!