Skip to content

Conversation

lindyhopchris
Copy link
Contributor

@lindyhopchris lindyhopchris commented Feb 4, 2021

Adds support for registering custom actions for resources. Although this deviates from the JSON:API spec, it is something that is regularly request or queried via Github issues.

Example:

$server->resource('posts')->actions('-actions', function ($actions) {
    $actions->delete('purge');
    $actions->withId()->post('publish');
});

Registers the following routes:

DELETE /api/v1/posts/-actions/purge
    -> PostController@purge
POST /api/v1/posts/{post}/-actions/publish
    -> PostController@publish

Supports all the usual methods - get, post, patch, put, delete and options.

It is recommended to use a prefix that avoids potential collisions with any future additions to the JSON:API spec - so in the example, we use /-actions instead of /actions. The prefix is optional, for example:

$server->resource('posts')->actions(function ($actions) {
    $actions->delete('purge');
    $actions->withId()->post('publish');
});

Registers the following routes:

DELETE /api/v1/posts/purge
POST /api/v1/posts/{post}/publish

The controller method name is camelised, but this can be overidden using the second method argument:

// PostController@fooBar
$actions->delete('foo-bar');

// PostController@foobar
$actions->delete('foo-bar', 'foobar');

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant