{"id":20081549,"url":"https://github.com/mixerapi/crud","last_synced_at":"2025-07-20T02:35:09.963Z","repository":{"id":57017877,"uuid":"382029097","full_name":"mixerapi/crud","owner":"mixerapi","description":"A CakePHP CRUD (Create/Read/Update/Delete) services for your RESTful APIs controller actions using CakePHP's dependency injection container. [READ ONLY]","archived":false,"fork":false,"pushed_at":"2024-03-25T23:50:24.000Z","size":49,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-02-13T02:38:22.291Z","etag":null,"topics":["calephp","crud","php","rest","rest-api"],"latest_commit_sha":null,"homepage":"https://mixerapi.com/plugins/crud","language":"PHP","has_issues":false,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/mixerapi.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-07-01T12:41:08.000Z","updated_at":"2024-02-17T22:19:30.000Z","dependencies_parsed_at":"2024-11-13T15:44:08.909Z","dependency_job_id":"42df7735-3563-40fe-9ef9-6e47fa5486db","html_url":"https://github.com/mixerapi/crud","commit_stats":{"total_commits":12,"total_committers":1,"mean_commits":12.0,"dds":0.0,"last_synced_commit":"98a020b606764759290727a92c4f531d71a5532e"},"previous_names":[],"tags_count":15,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mixerapi%2Fcrud","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mixerapi%2Fcrud/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mixerapi%2Fcrud/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mixerapi%2Fcrud/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mixerapi","download_url":"https://codeload.github.com/mixerapi/crud/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":241515938,"owners_count":19975139,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["calephp","crud","php","rest","rest-api"],"created_at":"2024-11-13T15:39:26.447Z","updated_at":"2025-03-02T13:41:02.173Z","avatar_url":"https://github.com/mixerapi.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# MixerApi CRUD\n\n![Stability][ico-stability]\n\n[![Version](https://img.shields.io/packagist/v/mixerapi/crud.svg?style=flat-square)](https://packagist.org/packages/mixerapi/crud)\n[![Build](https://github.com/mixerapi/mixerapi-dev/workflows/Build/badge.svg?branch=master)](https://github.com/mixerapi/mixerapi-dev/actions?query=workflow%3ABuild)\n[![Coverage](https://coveralls.io/repos/github/mixerapi/mixerapi-dev/badge.svg?branch=master)](https://coveralls.io/github/mixerapi/mixerapi-dev?branch=master)\n[![MixerApi](https://mixerapi.com/assets/img/mixer-api-red.svg)](https://mixerapi.com)\n[![CakePHP](https://img.shields.io/badge/cakephp-^4.2-red?logo=cakephp)](https://book.cakephp.org/4/en/index.html)\n[![Minimum PHP Version](https://img.shields.io/badge/php-^8.0-8892BF.svg?logo=php)](https://php.net/)\n\nThis plugin provides CRUD (Create/Read/Update/Delete) services to your RESTful APIs controller actions\nusing [CakePHP's dependency injection container](https://book.cakephp.org/4/en/development/dependency-injection.html).\n\n- Perform most crud operations with a single line of code.\n- Automatically serializes data into JSON, XML, etc.\n- Automatically enforces allowed requests `$this-request-\u003eallowMethod()`\n- Crud plays nicely with existing MixerApi plugins including Pagination and CakePHP Search.\n- Use of Interfaces allow you to use your own concrete implementations down the line.\n- Requires CakePHP ^4.2 compatible projects.\n\nYou may also want to look at [CakePHP Crud](https://crud.readthedocs.io/en/latest/installation.html) which doesn't\nrely on dependency injection. If you're using this plugin without MixerApi/ExceptionRender or for a non-API projects\n[read below](#other-usages).\n\n## Installation\n\n```console\ncomposer require mixerapi/crud\nbin/cake plugin load MixerApi/Crud\n```\n\nAlternatively after composer installing you can manually load the plugin in your Application:\n\n```php\n# src/Application.php\npublic function bootstrap(): void\n{\n    $this-\u003eaddPlugin('MixerApi/Crud');\n}\n```\n\nSee [Plugin Options](#plugin-options) for additional configurations.\n\n\n## Usage\n\nOnce enabled, the following services may be injected into your controller actions.\n\n```php\nuse MixerApi\\Crud\\Interfaces\\{CreateInterface, ReadInterface, UpdateInterface, DeleteInterface, SearchInterface};\n```\n\n| Interface       | Injected Service             | Use-cases          |\n|-----------------|------------------------------|--------------------|\n| CreateInterface | MixerApi\\Crud\\Service\\Create | `add()` actions    |\n| ReadInterface   | MixerApi\\Crud\\Service\\Read   | `view()` actions   |\n| UpdateInterface | MixerApi\\Crud\\Service\\Update | `edit()` actions   |\n| DeleteInterface | MixerApi\\Crud\\Service\\Delete | `delete()` actions |\n| SearchInterface | MixerApi\\Crud\\Service\\Search | `index()` actions  |\n\nAll Crud services infer the table name from the controller, you can change the table name by calling the\n`setTableName($name)` method.\n\nIf you are using MixerApi\\ExceptionRender then an event will catch validation errors and handle the response for you,\notherwise a `MixerApi\\Crud\\Exception\\ResourceWriteException` is thrown.\n\nSee below regarding [path parameters](#path-parameters) if your path parameter is not `id`.\n\n### Create\n\n```php\npublic function add(CreateInterface $create)\n{\n    $this-\u003eset('data', $create-\u003esave($this));\n}\n```\n\nNote, `save()` with `$options` is supported.\n\n```php\nreturn $create-\u003esave($this, [\n    'accessibleFields' =\u003e [\n        'password' =\u003e true,\n    ]\n]);\n```\n\n### Read\n\n```php\npublic function view(ReadInteface $read)\n{\n    $this-\u003eset('data', $read-\u003eread($this));\n}\n```\n\nNote, `read()` with `$options` is supported.\n\n```php\nreturn $read-\u003esave($this, ['contains' =\u003e ['OtherTable']]);\n```\n\nReturn a CakePHP `Query` object instead:\n\n```php\n$query = $read-\u003equery($this)\n```\n\n### Update\n\n```php\npublic function edit(UpdateInterface $update)\n{\n    $this-\u003eset('data', $update-\u003esave($this));\n}\n```\n\nNote, `update()` with `$options` is supported.\n\n```php\nreturn $update-\u003esave($this, [\n    'accessibleFields' =\u003e [\n        'password' =\u003e true,\n    ]\n]);\n```\n\n### Delete\n\n```php\npublic function delete(DeleteInterface $delete)\n{\n    return $delete-\u003edelete($this)-\u003erespond(); // calling respond() is optional\n}\n```\n\nNote, `delete()` with `$options` is supported.\n\n```php\nreturn $delete-\u003edelete($this, ['atomic' =\u003e false]);\n```\n\n### Search\n\nThe Search service works with [Pagination](https://book.cakephp.org/4/en/controllers/components/pagination.html) and\noptionally with [CakePHP Search](https://github.com/FriendsOfCake/search).\n\nExample:\n\n```php\npublic function index(SearchInterface $search)\n{\n    $this-\u003eset('data', $search-\u003esearch($this));\n}\n```\n\nTo use [CakePHP Search](https://github.com/FriendsOfCake/search) initialize the component as normal in your controllers\n`initialize()` method.\n\n```php\n$this-\u003eset('data', $search-\u003esearch($this));\n```\n\nFor custom CakePHP Search collections call the `setCollection($name)` method:\n\n```php\n$this-\u003eset('data', $search-\u003esetCollection('collection_name')-\u003esearch($this));\n```\n\nReturn a CakePHP `Query` object instead:\n\n```php\n$query = $search-\u003equery($this);\n```\n\n## Serialization\n\nSerialization is handled by a `Controller.beforeRender` listener. It serializes the first viewVar found for all CRUD\noperations and will not run for non-crud operations. See [Options](#plugin-options) for disabling serialization.\n\n## Allowed HTTP Methods\n\nAllowed methods is handled by a `Controller.initialize` listener. See [Plugin Options](#plugin-options) for disabling or\nmodifying the defaults.\n\n| Action   | HTTP method(s)       |\n|----------|----------------------|\n| index()  | GET                  |\n| view()   | GET                  |\n| add()    | POST                 |\n| edit()   | POST, PUT, and PATCH |\n| delete() | DELETE               |\n\nYou may also call `setAllowMethods($methods)` on any service to overwrite the default behavior. This accepts a string\nor any array as an argument just like the native `$request-\u003eallowedMethods()`.\n\n## Plugin Options\n\nYou may customize functionality by passing in an options array when adding the plugin.\n\n```php\n# src/Application.php\n\npublic function bootstrap(): void\n{\n    $this-\u003eaddPlugin('MixerApi/Crud', $options);\n}\n```\n\nCustomize allowed HTTP methods:\n\n```php\n$options = [\n    'allowedMethods' =\u003e [\n        'add' =\u003e ['post'],\n        'edit' =\u003e ['patch'],\n        'delete' =\u003e ['delete'],\n    ]\n];\n```\n\nTo disable automatic `$request-\u003eallowMethod()` entirely:\n\n```php\n$options = [\n    'allowedMethods' =\u003e []\n];\n```\n\nDisable automatic serialization:\n\n```php\n$options = [\n    'doSerialize' =\u003e false, // default is true\n];\n```\n\n## Misc\n\n#### Path Parameters\n\nIf your path parameter for the resource is not `id` then pass the identifier as the second argument:\n\n```php\npublic function view(ReadInteface $read, string $id)\n{\n    $this-\u003eset('data', $read-\u003eread($this, $id));\n}\n```\n\nThe above also works for Update and Delete.\n\n#### Other Usages\n\nThis plugin works best with API projects using MixerApi/ExceptionRender which uses events to set the response in the\nevent of an error. If your project isn't using ExceptionRender or you're not an API you can write a custom exception\nrenderer and look for `ResourceWriteException`, then alter the `viewVars` output using the `EntityInterface` from\n`ResourceWriteException::getEntity()`.\n\nRead the [CakePHP Custom ExceptionRenderer](https://book.cakephp.org/4/en/development/errors.html#custom-exceptionrenderer)\ndocumentation for more information.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmixerapi%2Fcrud","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmixerapi%2Fcrud","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmixerapi%2Fcrud/lists"}