Generate dummy API endpoints from a simple PHP array.

Overview

Laravel Fake API

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"
        }
    ]
}
You might also like...
A PHP notebook application build with PHP Symfony as back-end API and VueJS/Vuetify front-end.
A PHP notebook application build with PHP Symfony as back-end API and VueJS/Vuetify front-end.

PHPersonal Notes 📓 - BETA RELEASE PHPersonal notes is an application to store your personal notes! PHPersonalnotes is build with Symfony/VueJS/Vuetif

PHP Framework for building scalable API's on top of Laravel.
PHP Framework for building scalable API's on top of Laravel.

Apiato Build scalable API's faster | With PHP 7.2.5 and Laravel 7.0 Apiato is a framework for building scalable and testable API-Centric Applications

PHP Framework for building scalable API's on top of Laravel.
PHP Framework for building scalable API's on top of Laravel.

Apiato Build scalable API's faster | With PHP 7.2.5 and Laravel 7.0 Apiato is a framework for building scalable and testable API-Centric Applications

Exemplary RealWorld backend API built with Laravel PHP framework.
Exemplary RealWorld backend API built with Laravel PHP framework.

Example of a PHP-based Laravel application containing real world examples (CRUD, auth, advanced patterns, etc) that adheres to the RealWorld API spec.

A simple CRUD built in PHP, Bootstrap and MySQL
A simple CRUD built in PHP, Bootstrap and MySQL

✨ Notes-CRUD ✨ A simple CRUD built in PHP, Bootstrap and MySQL 📑 Table of Contents Usage Contribute Screenshots 🤖 Usage Add the project to your envi

Simple mobile shop using basic php

simple-mobile-shop simple mobile shop using basic php Project Overview Go to your Xammp/wamp-server root folder Then create a folder named "mobileshop

Simple skeleton for the PHP Slim framework

Simple skeleton for the PHP Slim framework

Simple CRUD Product dengan PHP, MySQL & Bootstrap 5
Simple CRUD Product dengan PHP, MySQL & Bootstrap 5

Simple CRUD Product dengan PHP, MySQL & Bootstrap 5 Instalasi Pastikan sudah menginstall XAMPP atau sejenisnya Nyalakan service apache dan mysql buka

Very simple CRUD project, written in pure php. Designed as framework-agnostic as possible, and with basically no stack overflow if you can believe that
Very simple CRUD project, written in pure php. Designed as framework-agnostic as possible, and with basically no stack overflow if you can believe that

briefly simple CRUD pure php project for self improvement I try to make it purely in github - not only code, but any documentation (wiki), tasks (issu

Comments
  • Unable to locate publishable resources.

    Unable to locate publishable resources.

    Howdy,

    I'm starting to work on an API for my application and thought this would be a good project to use in order to avoid lots of initla boilerplate. I was able to install the package just fine, but when I ran the publish command

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

    I got the following error:

    Unable to locate publishable resources.
    

    I'm using Lumen 8.0 if that makes a difference.

    opened by commadelimited 0
Releases(1.0.0)
Owner
Andy Abi Haidar
CTO / Managing Partner @ Yellow
Andy Abi Haidar
This package provides an artisan command to generate a basic crud with Restful API support

NHRROB Crud Generator Package This package provides an artisan command to generate a basic crud composer install command: composer require nhrrob/crud

Nazmul Hasan Robin 22 Jun 24, 2022
A prototype generator to quickly generate prototypes based on Neos nodetype definitions.

UpAssist.PrototypeGenerator What does this package do? This package lets you create scaffolding for prototypes for Neos using the commandline. Why did

UpAssist 6 Nov 8, 2021
Generate Admin Panels CRUDs and APIs in Minutes with tons of other features and customizations with 3 different themes

InfyOm Laravel Generator Generate Admin Panels CRUDs and APIs in Minutes with tons of other features and customizations with 3 different themes. Docum

InfyOmLabs (InfyOm Technologies) 3.5k Jan 1, 2023
A helper method to generate absolute asset URL's to Vite assets

Laravel vite() helper method A super simple Laravel helper to fill the void that Laravel Mix left. Mix had a helper, aptly named mix() that would retu

 MotoMediaLab 17 Dec 2, 2022
Laravel API starter Kit will provide you with the tools for making API's that everyone will love

Laravel API Starter Kit Laravel API starter Kit will provide you with the tools for making API's that everyone will love, API Authentication is alread

Jose Luis Fonseca 400 Dec 29, 2022
A Laravel 5.8 API Boilerplate to create a ready-to-use REST API in seconds.

Laravel API Boilerplate (JWT Edition) for Laravel 5.8 Laravel API Boilerplate is a "starter kit" you can use to build your first API in seconds. As yo

Francesco Malatesta 1.2k Dec 18, 2022
Hydra is a zero-config API boilerplate with Laravel Sanctum that comes with excellent user and role management API out of the box

Hydra - Zero Config API Boilerplate with Laravel Sanctum Hydra is a zero-config API boilerplate with Laravel Sanctum and comes with excellent user and

Hasin Hayder 858 Dec 24, 2022
Creating a simple weather web application with Laravel rest API.

Weather Channel Creating a website for weather status, with Laravel restAPI. See the Website Weather.Channel-1.mov Features REST API Invoake Controlle

AmirH.Najafizadeh 3 Jul 31, 2022
a simple and secured RESTful API made with codeIgniter and JSON Web Tokens

CodeIgniter 4 Application Starter What is CodeIgniter? CodeIgniter is a PHP full-stack web framework that is light, fast, flexible and secure. More in

null 2 Oct 8, 2021
Simple Laravel API with Sanctum Authentication.

Laravel API (with sanctum authentication) What is sanctum? Laravel Sanctum provides a featherweight authentication system for SPAs (single page applic

Nopal 2 Jul 14, 2022