andyabih/laravel-fake-api 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

andyabih/laravel-fake-api

Composer 安装命令:

composer require andyabih/laravel-fake-api

包简介

Generate dummy API endpoints from a simple PHP array. No more waiting for the backend to be done before working on the frontend.

README 文档

README

Create placeholder API endpoints from a simple PHP array.

LFA utilizes Faker for dummy data.

Inspired by JSON Server.

Installation

To install LFA, run the following composer command:

composer require andyabih/laravel-fake-api --dev

Next, publish the config file to fill in your endpoints & responses:

php artisan vendor:publish --provider="Andyabih\LaravelFakeApi\LaravelFakeApiServiceProvider" --tag="config"

Configuration

Below is a sample laravel-fake-api.php config file:

<?php

return [
    'base_endpoint' => '/api/fake',

    'endpoints' => [
        'posts' => [
            '_settings' => [
                'identifiable' => 'slug',
                'auth'         => true,
                'paginate'     => 5,
            ],
            
            'id'       => 'random_digit_not_null',
            'title'    => 'word',
            'slug'     => 'word',
            'text'     => 'paragraph|2',
            'category' => '_categories'
        ],

        'categories' => [
            'id'    => 'random_digit_not_null',
            'name'  => 'word',
            'image' => 'image_url'
        ],
    ]
];

Base endpoint

LFA registers the /api/fake/ prefix for all endpoints, you can change that by changing the base_endpoint entry in the configuration file.

Endpoints

Inside the endpoints array, you can create all your different endpoints. The example above contains two: /posts and /categories (/api/fake/posts for full).

Fields

For each endpoint, you can then specify all the fields you want the response to contain. We've defined 5 here: id, title, slug, text, and category. The values for these fields are Faker methods. Make sure you snake case them (ie: randomDigit becomes random_digit), and any additional argument you want to pass should be separated with a pipe; so "text" => 'paragraph|2' translates to paragraph(2).

In case you want to show a foreign entity inside the endpoint response, you can prefix it with an underscore. A plural key will return an array of multiple entities, and a singular one would return only one (ie: "categories" => "_categories" will return an array of categories, but "category" => "_category" will only return a single entry).

If you are requesting multiple relationship entities, you can also pass in an optional argument specifying the amount of results you want, so you can do something like "categories" => "_categories|5" which will return 5 categories.

Endpoint settings

A reserved _setting key is used to specify any additional settings to the endpoint. Currently, only 3 settings are available: identifiable, paginate, and auth.

Identifiable

The identifiable option will determine what column name is used to identify specific entries. The default value for the identifiable setting is id, meaning a GET call to /api/fake/categories/1 will check against the id field. In the above example, posts are identified by their slug, so you will need to access them using something like /api/fake/posts/post-slug-here.

Paginate

Straightforward here. If you are expecting multiple results, you can paginate the response by enabling the paginate option (which is by default false), and specify the amount of entries you want per page (so 5 in the example).

Auth

LFA also offers a 'fake' authentication layer. If enabled (it's false by default), you will receive a 401 unauthorized error if you do not call the endpoint with an Authorization header. No further checks are done on the token, it just checks if the header exists.

Filters

_count

You can pass in the query parameter _count to specify the number of results you want. Calling /api/fake/categories?_count=5 will return 5 categories.

_without

You can use the _without parameter to specify which columns you want to exclude. /api/fake/posts?_without=title will return posts without the title field.

_only

Same logic as _without, but this time you specify which columns you want to include. /api/fake/posts?_only=title will only return the title field for the posts.

_no_relationships

You can specify that you want to ignore all embedded relationships with the _no_relationships parameter. /api/fake/posts?_no_relationships=1 will not return the category entity inside the response.

Column name

You can also pass in a column name with a value to filter by value. /api/fake/posts?slug=slug-1 will only return entries where the slug field is equal to slug-1. Also works with relationships, so you can do something like /api/fake/posts?categories__id=1, the format for this is entityName__fieldName.

Preset responses

LFA checks for a laravel-fake-api.json file in the root of your Laravel project. If available, LFA will combine both the randomized dummy data & the preset responses in your JSON file.

A sample JSON file for the above configuration could be something like:

{
    "categories": [
        {
            "id": 1,
            "name": "Category 1",
            "image": "https://google.com/image.jpg"
        },
        {
            "id": 2,
            "name": "Category 2",
            "image": "https://google.com/image.jpg"
        },
        {
            "id": 3,
            "name": "Category 3",
            "image": "https://google.com/image.jpg"
        }
    ]
}

andyabih/laravel-fake-api 适用场景与选型建议

andyabih/laravel-fake-api 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 236 次下载、GitHub Stars 达 19, 最近一次更新时间为 2021 年 04 月 10 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 andyabih/laravel-fake-api 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 andyabih/laravel-fake-api 我们能提供哪些服务?
定制开发 / 二次开发

基于 andyabih/laravel-fake-api 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 236
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 19
  • 点击次数: 27
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 19
  • Watchers: 1
  • Forks: 2
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2021-04-10