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

Commit df63e7e

Browse files
committed
Merge pull request #163 from karborator/master
InjectTemplateListener - returnn api proper view
2 parents 4fcaa6d + 3ff4408 commit df63e7e

File tree

3 files changed

+31
-0
lines changed

3 files changed

+31
-0
lines changed

src/Controller/Plugin/AcceptableViewModelSelector.php

Lines changed: 3 additions & 0 deletions
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

Lines changed: 4 additions & 0 deletions
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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,4 +340,28 @@ public function testPrefersRouteMatchController()
340340

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

0 commit comments

Comments
 (0)