Home  >  Article  >  PHP Framework  >  Laravel extension developed based on hprose/hprose-php: Introduction to laravel-hprosed

Laravel extension developed based on hprose/hprose-php: Introduction to laravel-hprosed

不言
不言Original
2018-08-28 17:01:043050browse

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

Version requirements

Laravel>=5.3

Install

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

or edit composer. json

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

Configuration

  1. 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,
]
  1. 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
  1. 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

Use

Routing

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 &#39;update name: &#39; . $name;
    }
}

Client call

$client->getUserByName(&#39;zhuqipeng&#39;);
$client->userUpdate(&#39;zhuqipeng&#39;);

Routing group

\LaravelHproseRouter::group(array $attributes, callable $callback);
  • array $attributes attributes ['namespace' => '', 'prefix' => '']

  • callable $callback callback function

\LaravelHproseRouter::group([&#39;namespace&#39; => &#39;App\Controllers&#39;], function ($route) {
    $route->add(&#39;getUserByName&#39;, function ($name) {
        return &#39;name: &#39; . $name;
    });

    $route->add(&#39;userUpdate&#39;, &#39;User@update&#39;);
});

Client call

$client->getUserByName(&#39;zhuqipeng&#39;);
$client->userUpdate(&#39;zhuqipeng&#39;);

Prefix

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

    $route->add(&#39;update&#39;, &#39;User@update&#39;);
});

Client call

$client->user->getByName(&#39;zhuqipeng&#39;);
$client->user->update(&#39;zhuqipeng&#39;);
// 或者
$client->user_getByName(&#39;zhuqipeng&#39;);
$client->user_update(&#39;zhuqipeng&#39;);

Start the service

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn