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

Commit ada9ca1

Browse files
committed
Merge branch 'hotfix/163'
Close #163
2 parents 4fcaa6d + bdf5b04 commit ada9ca1

File tree

4 files changed

+68
-6
lines changed

4 files changed

+68
-6
lines changed

CHANGELOG.md

+31-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,37 @@ All notable changes to this project will be documented in this file, in reverse
66

77
### Added
88

9-
- Nothing.
9+
- [#163](https://github.com/zendframework/zend-mvc/pull/163) adds support to the
10+
`AcceptableViewModelSelector` plugin for controller maps in the `view_manager`
11+
configuration in the format:
12+
13+
```php
14+
[
15+
'ControllerClassName' => 'view/name',
16+
]
17+
```
18+
19+
This fixes an issue observed when running with Apigility.
20+
21+
- [#163](https://github.com/zendframework/zend-mvc/pull/163) adds support to the
22+
`InjectTemplateListener` for specifying whether or not to prefer the
23+
controller matched during routing via routing configuration:
24+
25+
```php
26+
'route-name' => [
27+
/* ... */
28+
'options' => [
29+
/* ... */
30+
'defaults' => [
31+
/* ... */
32+
'prefer_route_match_controller' => true,
33+
],
34+
],
35+
],
36+
```
37+
38+
This allows actions that might otherwise skip injection of the template
39+
to force the injection.
1040

1141
### Deprecated
1242

src/Controller/Plugin/AcceptableViewModelSelector.php

+3
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,9 @@ public function getDefaultMatchAgainst()
209209
protected function injectViewModelName($modelAcceptString, $modelName)
210210
{
211211
$modelName = str_replace('\\', '|', $modelName);
212+
$modelAcceptString = (is_array($modelAcceptString))
213+
? $modelAcceptString[key($modelAcceptString)]
214+
: $modelAcceptString;
212215
return $modelAcceptString . '; ' . self::INJECT_VIEWMODEL_NAME . '="' . $modelName . '", ';
213216
}
214217

src/View/Http/InjectTemplateListener.php

+4
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,10 @@ public function injectTemplate(MvcEvent $e)
6161
}
6262

6363
$routeMatch = $e->getRouteMatch();
64+
if ($preferRouteMatchController = $routeMatch->getParam('prefer_route_match_controller', false)) {
65+
$this->setPreferRouteMatchController($preferRouteMatchController);
66+
}
67+
6468
$controller = $e->getTarget();
6569
if (is_object($controller)) {
6670
$controller = get_class($controller);

test/View/InjectTemplateListenerTest.php

+30-5
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Zend\Router\RouteMatch;
1818
use Zend\Mvc\View\Http\InjectTemplateListener;
1919
use Zend\View\Model\ViewModel;
20+
use ZendTest\Mvc\Controller\TestAsset\SampleController;
2021

2122
class InjectTemplateListenerTest extends TestCase
2223
{
@@ -108,7 +109,7 @@ public function testBypassesTemplateInjectionIfResultViewModelAlreadyHasATemplat
108109
public function testMapsSubNamespaceToSubDirectory()
109110
{
110111
$myViewModel = new ViewModel();
111-
$myController = new \ZendTest\Mvc\Controller\TestAsset\SampleController();
112+
$myController = new SampleController();
112113
$this->event->setTarget($myController);
113114
$this->event->setResult($myViewModel);
114115

@@ -158,7 +159,7 @@ public function testMapsSubNamespaceToSubDirectoryWithControllerFromEventTarget(
158159
$moduleRouteListener->onRoute($this->event);
159160

160161
$myViewModel = new ViewModel();
161-
$myController = new \ZendTest\Mvc\Controller\TestAsset\SampleController();
162+
$myController = new SampleController();
162163

163164
$this->event->setTarget($myController);
164165
$this->event->setResult($myViewModel);
@@ -183,7 +184,7 @@ public function testMapsSubNamespaceToSubDirectoryWithControllerFromEventTargetS
183184
$template1 = $myViewModel->getTemplate();
184185

185186
$myViewModel = new ViewModel();
186-
$myController = new \ZendTest\Mvc\Controller\TestAsset\SampleController();
187+
$myController = new SampleController();
187188

188189
$this->event->setTarget($myController);
189190
$this->event->setResult($myViewModel);
@@ -204,7 +205,7 @@ public function testControllerMatchedByMapIsInflected()
204205

205206
$this->listener->setControllerMap(['ZendTest' => true]);
206207
$myViewModel = new ViewModel();
207-
$myController = new \ZendTest\Mvc\Controller\TestAsset\SampleController();
208+
$myController = new SampleController();
208209
$this->event->setTarget($myController);
209210
$this->event->setResult($myViewModel);
210211

@@ -332,12 +333,36 @@ public function testPrefersRouteMatchController()
332333
$this->listener->setPreferRouteMatchController(true);
333334
$this->routeMatch->setParam('controller', 'Some\Other\Service\Namespace\Controller\Sample');
334335
$myViewModel = new ViewModel();
335-
$myController = new \ZendTest\Mvc\Controller\TestAsset\SampleController();
336+
$myController = new SampleController();
336337

337338
$this->event->setTarget($myController);
338339
$this->event->setResult($myViewModel);
339340
$this->listener->injectTemplate($this->event);
340341

341342
$this->assertEquals('some/other/service/namespace/sample', $myViewModel->getTemplate());
342343
}
344+
345+
public function testPrefersRouteMatchControllerWithRouteMatchAndControllerMap()
346+
{
347+
$this->assertFalse($this->listener->isPreferRouteMatchController());
348+
$controllerMap = [
349+
'Some\Other\Service\Namespace\Controller\Sample' => 'another/sample'
350+
];
351+
352+
$this->routeMatch->setParam('prefer_route_match_controller', true);
353+
$this->routeMatch->setParam('controller', 'Some\Other\Service\Namespace\Controller\Sample');
354+
355+
$preferRouteMatchControllerRouteMatchConfig = $this->routeMatch->getParam('prefer_route_match_controller', false);
356+
$this->listener->setPreferRouteMatchController($preferRouteMatchControllerRouteMatchConfig);
357+
$this->listener->setControllerMap($controllerMap);
358+
359+
$myViewModel = new ViewModel();
360+
$myController = new SampleController();
361+
362+
$this->event->setTarget($myController);
363+
$this->event->setResult($myViewModel);
364+
$this->listener->injectTemplate($this->event);
365+
366+
$this->assertEquals('another/sample', $myViewModel->getTemplate());
367+
}
343368
}

0 commit comments

Comments
 (0)