Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add doc about actions #161

Merged
merged 2 commits into from
May 6, 2014
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 75 additions & 0 deletions docs/07. Cookbook.md
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,81 @@ class CustomUserHydrator extends \Zend\Stdlib\Hydrator\ClassMethods
You finally just need to add this custom hydrator to the hydrator plugin manager, and writing the corresponding
factory.

## How to deal with actions?

While REST architecture simplifies the creation of APIs, going "the RESTful way" at all costs is often a bad idea. An
example is actions. This does not map well to a REST architecture: think about a post that you want to like or unlike.

If you want to go 100% RESTful, you have two choices: you could either use a custom HTTP verb (like LIKE and UNLIKE), but
this is not supported by all browsers and CDN, or you could go with a "like" sub-resources, so you could add a like
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CDNs

using the following URL: POST `posts/4/likes` and removing it using the following URL: DELETE `posts/4/likes/64`.

However, this really complicates your back-end for very few benefits. In those cases, the simplest is to use simpler
actions. ZfrRest does not provide any mechanism out-of-the box, so what you should do is simply using standard
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

standard controllers and routes

controllers that extend `Zend\Mvc\Controller\AbstractActionController`:

```php
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The section below here is not needed - this is redundant with zf2 base knowledge.

use Zend\Mvc\Controller\AbstractActionController;

class LikePostController extends AbstractActionController
{
public function likeAction()
{
// Do things
}

public function unlikeAction()
{
// Do things
}
}
```

Then, define specific routes in your config files:

```php
'router' => [
'routes' => [
'action-posts' => [
'type' => 'Segment',
'options' => [
'route' => '/posts/:post_id',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't use :action?

'defaults' => [
'controller' => LikePostController::class
],
'constraints' => [
'post_id' => '[0-9]+'
]
],
'priority' => 2, // Make sure it's evaluated BEFORE REST route
'child_routes' => [
'like' => [
'type' => 'Literal',
'options' => [
'route' => '/like',
'defaults' => [
'action' => 'like'
]
]
],

'unlike' => [
'type' => 'Literal',
'options' => [
'route' => '/unlike',
'defaults' => [
'action' => 'unlike'
]
]
]
]
]
]
]
```

> Please note the priority parameter. This is used so that those routes are actually evaluated BEFORE any REST routes.

## Tuning ZfrRest for production

For maximum performance, here are a few best practices:
Expand Down
3 changes: 2 additions & 1 deletion docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ If you are looking for some information that is not listed in the documentation,
4. [How to specify a custom input filter?](/docs/07. Cookbook.md)
5. [How to filter a collection using query params?](/docs/07. Cookbook.md)
6. [How to serialize custom data that do not belong to the entity?](/docs/07. Cookbook.md)
7. [Tuning ZfrRest for production](/docs/07. Cookbook.md)
7. [How to deal with actions?](/docs/07. Cookbook.md)
8. [Tuning ZfrRest for production](/docs/07. Cookbook.md)

8. [Mapping reference](/docs/08. Mapping reference.md)
1. [Annotations](/docs/08. Mapping reference.md)