@@ -40,34 +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
+ \Zend\View\Model\ViewModel::class => [
48
+ 'text/html',
49
+ 'application/xhtml+xml',
50
+ ],
51
+ \Zend\View\Model\JsonModel::class => [
52
+ 'application/json',
53
+ 'application/javascript',
54
+ ],
55
+ \Zend\View\Model\FeedModel::class => [
56
+ 'application/rss+xml',
57
+ 'application/atom+xml',
58
+ ],
59
+ ];
60
+
61
+ public function apiAction()
62
+ {
63
+ $viewModel = $this->acceptableViewModelSelector($this->acceptCriteria);
64
+
65
+ // Potentially vary execution based on model returned
66
+ if ($viewModel instanceof \Zend\View\Model\JsonModel) {
67
+ // ...
68
+ }
69
+ }
65
70
}
66
71
```
67
72
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
70
- is met. Rules are matched in order, with the first match "winning."
73
+ The above would return a standard ` Zend\View\Model\ViewModel ` instance if no
74
+ criterias are met, and the specified view model types if a specific criteria
75
+ is met. Rules are matched in order, with the first match "winning".
76
+ Make sure to put your fallback view model * first* as a fallback for unknown
77
+ content types or ` */* ` .
78
+
79
+ > Browsers are sending ` */* ` as last content type of the Accept header so you have to define every
80
+ > acceptable view model and their content type.
71
81
72
82
## Forward Plugin
73
83
0 commit comments