Skip to content
This repository has been archived by the owner on Jan 29, 2020. It is now read-only.

Commit

Permalink
Merge branch 'hotfix/195'
Browse files Browse the repository at this point in the history
Close #195
  • Loading branch information
weierophinney committed Aug 29, 2016
2 parents a370fb6 + ca6d780 commit 7e1d0f9
Showing 1 changed file with 32 additions and 22 deletions.
54 changes: 32 additions & 22 deletions doc/book/plugins.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,34 +40,44 @@ As an example:

```php
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\JsonModel;

class SomeController extends AbstractActionController
{
protected $acceptCriteria = [
'Zend\View\Model\JsonModel' => [
'application/json',
],
'Zend\View\Model\FeedModel' => [
'application/rss+xml',
],
];

public function apiAction()
{
$viewModel = $this->acceptableViewModelSelector($this->acceptCriteria);

// Potentially vary execution based on model returned
if ($viewModel instanceof JsonModel) {
// ...
}
}
protected $acceptCriteria = [
\Zend\View\Model\ViewModel::class => [
'text/html',
'application/xhtml+xml',
],
\Zend\View\Model\JsonModel::class => [
'application/json',
'application/javascript',
],
\Zend\View\Model\FeedModel::class => [
'application/rss+xml',
'application/atom+xml',
],
];

public function apiAction()
{
$viewModel = $this->acceptableViewModelSelector($this->acceptCriteria);

// Potentially vary execution based on model returned
if ($viewModel instanceof \Zend\View\Model\JsonModel) {
// ...
}
}
}
```

The above would return a standard `Zend\View\Model\ViewModel` instance if the
criteria is not met, and the specified view model types if the specific criteria
is met. Rules are matched in order, with the first match "winning."
The above would return a standard `Zend\View\Model\ViewModel` instance if no
criterias are met, and the specified view model types if a specific criteria
is met. Rules are matched in order, with the first match "winning".
Make sure to put your fallback view model *first* as a fallback for unknown
content types or `*/*`.

> Browsers are sending `*/*` as last content type of the Accept header so you have to define every
> acceptable view model and their content type.
## Forward Plugin

Expand Down

0 comments on commit 7e1d0f9

Please sign in to comment.