定制 mrlijan/restmold 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

mrlijan/restmold

最新稳定版本:1.0.0

Composer 安装命令:

composer require mrlijan/restmold

包简介

Restmold, Model-based HTTP client for Laravel applications.

README 文档

README

Restmold (pronounced /restː/ /mold:/) is a Laravel package for modeling 3rd party HTTP services and as an HTTP client. Using this package will allow you to model your external HTTP services. The magic behind Restmold is that you don't need to repeatedly re-code your API class services.

Inspiration

The inspiration for this project started when the company I worked for migrated our giant monolith into a bunch of small microservices. Time passed, and we found ourselves writing API services repeatedly as the number of services grew. So, inspired by the flow of Laravel's models, Restmold has born out.


⚠️ The package supports Laravel 7 and above.


Installation

composer require mrlijan/restmold

Usage

Restmold is so easy-to-use; you just need to run the generate command to create a new API model. As a result, a new folder named "ApiModels" will be created under the \App folder, including the new concrete class.

php artisan restmold:generate <service name>



Class Structure

Each restmold concrete class will derive its props and methods from the RestModel abstract class and should also implement the following properties and methods to work properly:

Type Name Description
Property baseURI The base uri for that specific service
Method headers Return the request's headers. This can be used for authentication
Method routes Used to construct the service's structure



Class Configuration

the routes method is being used to construct the service's structure. This means that every array index listed below will be determined as a method for this service.

Each index of that array will be constructed as follows:

protected function routes(): array
{
    return [
        'methodName' => [
            'method' => 'GET', // The reuqest's method
            'path' => '/index' // The reuqest's endpoint
        ],
    ];
}

Config with Query Params

Your route includes query params? We've got your back! Just use the regular syntax, excluding the values. For example:

protected function routes(): array
{
    return [
        'list' => [
            'method' => 'GET',
            'path' => '/students?name&age'
        ],
    ];
}



Implementations

Using Query Params

Once everything is ready and configured, the service is prepared to use.

use App\ApiModels\StudentsAPIModel;

class StudentsController extends BaseController
{
    public function listByParams(StudentsAPIModel $students_api)
    {
        return $students_api->list([
            'query' => [
                'name' => 'Brendon',
                'age' => '22'
            ]
        ]);
    }
}

Using request body

use App\ApiModels\StudentsAPIModel;

class StudentsController extends BaseController
{
    public function createNewStudent(StudentsAPIModel $students_api)
    {
        return $students_api->create([
            'body' => [
                'firstName' => 'John',
                'lastName' => 'Dow'
                'age' => '24',
                'email' => 'john.d@example.com'
            ]
        ]);
    }
}



Piping Requests & Responses

Sometimes, collective pipes are necessary in some services, and for these cases, a request and response pipes have been created. In order to use them just override the following methods:

protected function requestPipe(Request $request): Request
{
    // ... manipulate the data, and return the request
    return $request;
}


protected function responsePipe(Response $response): Response
{
    // ... manipulate the data, and return the response
    return $response;
}

统计信息

  • 总下载量: 4
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 5
  • 点击次数: 2
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 5
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-09-22

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固