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

Commit ebbd53d

Browse files
Clarify AcceptableViewModelSelector parsing and browser behavior
1 parent ede64a1 commit ebbd53d

File tree

1 file changed

+30
-21
lines changed

1 file changed

+30
-21
lines changed

doc/book/plugins.md

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -40,35 +40,44 @@ As an example:
4040

4141
```php
4242
use Zend\Mvc\Controller\AbstractActionController;
43-
use Zend\View\Model\JsonModel;
4443

4544
class SomeController extends AbstractActionController
4645
{
47-
protected $acceptCriteria = [
48-
'Zend\View\Model\JsonModel' => [
49-
'application/json',
50-
],
51-
'Zend\View\Model\FeedModel' => [
52-
'application/rss+xml',
53-
],
54-
];
55-
56-
public function apiAction()
57-
{
58-
$viewModel = $this->acceptableViewModelSelector($this->acceptCriteria);
59-
60-
// Potentially vary execution based on model returned
61-
if ($viewModel instanceof JsonModel) {
62-
// ...
63-
}
64-
}
46+
protected $acceptCriteria = [
47+
// Make sure ViewModel is the first as a fallback
48+
\Zend\View\Model\ViewModel::class => [
49+
'text/html',
50+
'application/xhtml+xml',
51+
],
52+
\Zend\View\Model\JsonModel::class => [
53+
'application/json',
54+
'application/javascript'
55+
],
56+
\Zend\View\Model\FeedModel::class => [
57+
'application/rss+xml',
58+
'application/atom+xml',
59+
],
60+
];
61+
62+
public function apiAction()
63+
{
64+
$viewModel = $this->acceptableViewModelSelector($this->acceptCriteria);
65+
66+
// Potentially vary execution based on model returned
67+
if ($viewModel instanceof \Zend\View\Model\JsonModel) {
68+
// ...
69+
}
70+
}
6571
}
6672
```
6773

68-
The above would return a standard `Zend\View\Model\ViewModel` instance if the
69-
criteria is not met, and the specified view model types if the specific criteria
74+
The above would return a standard `Zend\View\Model\ViewModel` instance if no
75+
criterias are met, and the specified view model types if a specific criteria
7076
is met. Rules are matched in order, with the first match "winning."
7177

78+
> Browsers are sending `*/*` as last part of the Accept header so you have to define every
79+
> acceptable view model and their Accept-Header part. Otherwise the first view model will be used.
80+
7281
## Forward Plugin
7382

7483
Occasionally, you may want to dispatch additional controllers from within the

0 commit comments

Comments
 (0)