egretos/restful-model

易于实现的任何RESTful API的RESTful API模型

dev-master 2021-06-21 10:28 UTC

This package is auto-updated.

Last update: 2024-09-21 17:01:20 UTC


README

介绍

此包提供了一种使用Eloquent-like "模型"的REST HTTP服务的对象映射器。当使用Restful Model时,第三方API和REST资源都有一个用于集成的"模型"反射。Restful Model允许您通过HTTP请求读取、更新、删除资源,并且像Eloquent处理数据库一样轻松地修改它们。

安装

通过composer安装包

composer require egretos/restful-model

发布 rest_connections.php 配置

php artisan vendor:publish --provider="Egretos\RestModel\RestModelServiceProvider"

配置

开始之前,您需要定义您的第一个连接。所有HTTP连接都存储在config/rest_connections.php文件中。每个连接都需要domain配置才能工作。

这是一个最简单的连接配置

<?php
return [
    // Connection which will be used for models by default
    'default_connection' => env('REST_CONNECTION', 'base_connection'),

    'connections' => [
        'base_connection' => [
            'domain' => env('REST_DOMAIN', 'http://jsonplaceholder.typicode.com'),
            'content-type' => 'www-form', // Data format for PUT and POST requests. Available: x-www-form-urlencoded, json
        ],
    ],
];

模型

模型定义

定义您的模型。Restful Model与Eloquent Model的结构非常相似。在这里,我们创建一个Post模型来处理post HTTP RESTful资源:http://jsonplaceholder.typicode.com/posts

use Egretos\RestModel\Model;

class Post extends Model
{

}

资源名称

该模型将使用资源posts,这是模型复数名词的名称。当需要时,您可以使用$model->resource使用自己的资源名称。

http://jsonplaceholder.typicode.com/comments

use Egretos\RestModel\Model;

class Reviews extends Model
{
    protected $resource = 'comments';
}

RESTful连接

当您的模型使用非默认连接时,只需将第二个连接添加到配置中,并告诉模型即可。

rest_connections.php:

<?php
return [
    // Connection which will be used for models by default
    'default_connection' => env('REST_CONNECTION', 'base_connection'),

    'connections' => [
        'base_connection' => [
            'domain' => env('REST_DOMAIN', 'http://jsonplaceholder.typicode.com'),
            'content-type' => 'www-form', // Data format for PUT and POST requests. Available: www-form, x-www-form-urlencoded, json
        ],
        
        'payment_api' => [
            'domain' => env('PAYMENT_DOMAIN', 'http://paypaypay.pay.pay'),
        ],
    ],
];

http://paypaypay.pay.pay/payment 的模型类

use Egretos\RestModel\Model;

class Payment extends Model
{
    public $connection = 'payment_api';
}

HTTP查询

模型用于进行HTTP请求并将数据映射到模型对象中。模型使用内置的HTTP查询构建器,允许您构建所需的任何查询。

我们在这里从API获取所有帖子并打印第一个帖子的标题作为响应。链接是GET http://jsonplaceholder.typicode.com/posts

/** GET http://jsonplaceholder.typicode.com/posts */
foreach (Post::all() as $post) {
    echo $post->title; // sunt aut facere repellat...
}

检索单个模型

当您需要获取一个模型时,以下方法应有所帮助

/** GET http://jsonplaceholder.typicode.com/posts/1 */
Post::find(1);

/** GET http://jsonplaceholder.typicode.com/posts?title=Lorem */
Post::where('title', 'Lorem')->first();

HTTP查询构建器

all 方法将对/posts资源执行GET请求,然后将其映射到Post对象。当需要修改请求时,可以使用类似于Laravel Eloquent查询构建器的查询构建器。

use Egretos\RestModel\Request;

    $posts = Post::query()
        ->addHeader('Content-Language', 'en') // Puts new header to request
        ->setMethod(Request::METHOD_OPTIONS) // Set OPTIONS request method
        ->where('title', 'Lorem') // Sets query `title` param to `Lorem`
        ->send(); // Send a request. Here we get a response

保存模型

您可以使用$model->save()方法,就像在Eloquent中一样。如果模型存在,此操作将执行update(),如果模型不存在,则执行create

创建情况

$post = new Post;
$post->title = 'This title will be updated';

/** POST http://jsonplaceholder.typicode.com/posts */
$post->save();

更新情况

/** GET http://jsonplaceholder.typicode.com/posts/1 */
$post = Post::find(1);
$post->title = 'This title will be updated';

/** PUT http://jsonplaceholder.typicode.com/posts/1 */
$post->save();

创建和更新操作

save()方法在某些情况下可能不可预测或不清晰,因此存在createupdate()方法。

create方法将发送带有模型属性的POST请求

$post = new Post;
$post->title = 'This title will be updated';

/** POST http://jsonplaceholder.typicode.com/posts */
$post->create();

update方法将对带有id结尾的路由执行PUT请求

/** GET http://jsonplaceholder.typicode.com/posts/1 */
$post = Post::find(1);
$post->title = 'This title will be updated';

/** PUT http://jsonplaceholder.typicode.com/posts/1 */
$post->update();

first或find或

处理模型属性(批量赋值 + JSON)

最后请求和最后响应

删除模型(+ 查询)

复制模型

配置

处理HTTP异常