https://github.com/mixerapi/rest
Generate restful routes with a single command or expose them automatically with an AutoRouter [READ-ONLY]
https://github.com/mixerapi/rest
cakephp cakephp-api cakephp-bake cakephp-plugin cakephp-rest cakephp4 php rest rest-api restful restful-api
Last synced: 2 months ago
JSON representation
Generate restful routes with a single command or expose them automatically with an AutoRouter [READ-ONLY]
- Host: GitHub
- URL: https://github.com/mixerapi/rest
- Owner: mixerapi
- License: other
- Created: 2020-07-01T00:22:32.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2024-03-25T23:50:18.000Z (over 1 year ago)
- Last Synced: 2024-10-15T02:32:02.247Z (about 1 year ago)
- Topics: cakephp, cakephp-api, cakephp-bake, cakephp-plugin, cakephp-rest, cakephp4, php, rest, rest-api, restful, restful-api
- Language: PHP
- Homepage: https://mixerapi.com/plugins/rest
- Size: 109 KB
- Stars: 3
- Watchers: 2
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# MixerApi REST
[](https://packagist.org/packages/mixerapi/cakephp-rest)
[](https://github.com/mixerapi/mixerapi-dev/actions?query=workflow%3ABuild)
[](https://coveralls.io/github/mixerapi/mixerapi-dev?branch=master)
[](https://mixerapi.com)
[](https://book.cakephp.org/4/en/index.html)
[](https://php.net/)
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](https://github.com/mixerapi/bake). Check the official
[RESTful routing](https://book.cakephp.org/4/en/development/routing.html#restful-routing) documentation
for handling advanced routing scenarios not covered by this plugin.
Read more at [MixerAPI.com](https://mixerapi.com).
## Installation
!!! info ""
You can skip this step if MixerAPI is installed.
```console
composer require mixerapi/rest
bin/cake plugin load MixerApi/Rest
```
Alternatively after composer installing you can manually load the plugin in your Application:
```php
# 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`:
```php
# 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:
```php
# 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.
```console
# writes to `config/routes.php`
bin/cake mixerapi:rest route create
```
Use `--prefix` to specify a prefix:
```console
bin/cake mixerapi:rest route create --prefix /api
```
Use `--plugin` for plugins:
```console
# writes to `plugins/MyPlugin/config/routes.php`
bin/cake mixerapi:rest route create --plugin MyPlugin
```
To perform a dry-run use the `--display` option:
```console
bin/cake mixerapi:rest route create --display
```
For non-CRUD routes, sub-resources, and advanced routing please reference the CakePHP
[RESTful routing](https://book.cakephp.org/4/en/development/routing.html#restful-routing) documentation
### List Routes
This works similar to `bin/cake routes` but shows only RESTful routes and improves some formatting of information.
```console
bin/cake mixerapi:rest route list
```
To limit output to a specific plugin use the `--plugin` option:
```console
# 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:
```php
return [
'MixerApi' => [
'Rest' => [
'crud' => [
'statusCodes' => [
'index' => 200,
'view' => 200,
'add' => 201,
'edit' => 200,
'delete' => 204
]
]
]
]
];
```
See the CakePHP documentation on
[loading configuration files](https://book.cakephp.org/4/en/development/configuration.html#loading-additional-configuration-files)