Home > PHP Framework > Laravel > body text

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

不言
Release: 2018-08-28 17:01:04
Original
3060 people have browsed it

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
Copy after login

Install

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

or edit composer. json

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

Configuration

  1. Register ServiceProvider and Facade in config/app.php (Laravel 5.5 does not require manual registration)

'providers' => [
    // ...

    Zhuqipeng\LaravelHprose\ServiceProvider::class,
]
Copy after login
'aliases' => [
    // ...

    'LaravelHproseMethodManage' => Zhuqipeng\LaravelHprose\Facades\HproseMethodManage::class,
]
Copy after login
  1. Configuration.env file

List of listening addresses, string json format array

HPROSE_URIS=["tcp://0.0.0.0:1314"]
Copy after login

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
Copy after login
  1. CreateConfiguration and Route Files:

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

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
Copy after login

Add routing method

\LaravelHproseRouter::add(string $name, string|callable $action, array $options = []);
Copy after login
  • 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]);
Copy after login

Controller

<?php

namespace App\Controllers;

class User
{
    public function update($name)
    {
        return &#39;update name: &#39; . $name;
    }
}
Copy after login

Client call

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

Routing group

\LaravelHproseRouter::group(array $attributes, callable $callback);
Copy after login
  • 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;);
});
Copy after login

Client call

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

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;);
});
Copy after login

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;);
Copy after login

Start the service

php artisan hprose:socket_server
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template