Skip to content

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

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