-
Notifications
You must be signed in to change notification settings - Fork 31
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
controllers that extend `Zend\Mvc\Controller\AbstractActionController`: | ||
|
||
```php | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can't use |
||
'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: | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
CDNs