@@ -40,35 +40,44 @@ As an example:
40
40
41
41
``` php
42
42
use Zend\Mvc\Controller\AbstractActionController;
43
- use Zend\View\Model\JsonModel;
44
43
45
44
class SomeController extends AbstractActionController
46
45
{
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
+ }
65
71
}
66
72
```
67
73
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
70
76
is met. Rules are matched in order, with the first match "winning."
71
77
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
+
72
81
## Forward Plugin
73
82
74
83
Occasionally, you may want to dispatch additional controllers from within the
0 commit comments