Code Monkey home page Code Monkey logo

rest's Introduction

MixerApi REST

Latest Version on Packagist Build Coverage Status MixerApi CakePHP Minimum PHP Version

This plugin gets your API project up and going quickly by creating routes for you.

  • Build your routes.php file from a single command or automatically expose RESTful CRUD routes with a handy AutoRouter.
  • Set default HTTP status codes for CRUD operations

This plugin assumes you have already created models and controllers. For help with the latter check out MixerApi/Bake. Check the official RESTful routing documentation for handling advanced routing scenarios not covered by this plugin.

Read more at MixerAPI.com.

Installation

!!! info "" You can skip this step if MixerAPI is installed.

composer require mixerapi/rest
bin/cake plugin load MixerApi/Rest

Alternatively after composer installing you can manually load the plugin in your Application:

# src/Application.php
public function bootstrap(): void
{
    // other logic...
    $this->addPlugin('MixerApi/Rest');
}

AutoRouter

Creating routes is already pretty easy, but AutoRouter makes building CRUD routes effortless. This is great if you are just getting started with building APIs in CakePHP.

In your routes.php simply add \MixerApi\Rest\Lib\AutoRouter:

# config/routes.php
$routes->scope('/', function (RouteBuilder $builder) {
    // ... other routes
    (new AutoRouter($builder))->buildResources();
    // ... other routes
});

This will add routes for CRUD controller actions (index, add, edit, view, and delete). If your controller does not have any CRUD methods, then the route will be skipped. AutoRouting works for plugins too:

# in your plugins/{PluginName}/routes.php file
(new AutoRouter($builder, new ResourceScanner('MyPlugin\Controller')))->buildResources();

Create Routes

While AutoRouter makes life easy, it must scan your controllers to build RESTful resources. This has a slight performance penalty. No worry, you can use mixerapi:rest route create to code your routes for you. This will write routes directly to your routes.php file.

# writes to `config/routes.php`
bin/cake mixerapi:rest route create

Use --prefix to specify a prefix:

bin/cake mixerapi:rest route create --prefix /api

Use --plugin for plugins:

# writes to `plugins/MyPlugin/config/routes.php`
bin/cake mixerapi:rest route create --plugin MyPlugin

To perform a dry-run use the --display option:

bin/cake mixerapi:rest route create --display

For non-CRUD routes, sub-resources, and advanced routing please reference the CakePHP RESTful routing documentation

List Routes

This works similar to bin/cake routes but shows only RESTful routes and improves some formatting of information.

bin/cake mixerapi:rest route list

To limit output to a specific plugin use the --plugin option:

# limit to a plugin:
bin/cake mixerapi:rest route list --plugin MyPlugin

#limit to main application:
bin/cake mixerapi:rest route list --plugin App

CRUD HTTP Status Codes

The default status codes are:

Action Status Code
index 200
view 200
add 201
edit 200
delete 204

To change these load a MixerApi.Rest.crud.statusCodes configuration:

return [
    'MixerApi' => [
        'Rest' => [
            'crud' => [
                'statusCodes' => [
                    'index' => 200,
                    'view' => 200,
                    'add' => 201,
                    'edit' => 200,
                    'delete' => 204
                ]
            ]
        ]
    ]
];

See the CakePHP documentation on loading configuration files

rest's People

Contributors

cnizzardini avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

robitmoh masakni

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.