public function indexAction() { try { $object = $this->getUuidManager()->getUuid($this->params('uuid'), true); } catch (NotFoundException $e) { $this->getResponse()->setStatusCode(404); return false; } $normalized = $this->getNormalizer()->normalize($object); $routeName = $normalized->getRouteName(); $routeParams = $normalized->getRouteParams(); $type = $normalized->getType(); $url = $this->url()->fromRoute($routeName, $routeParams, null, null, false); if (!$this->getRequest()->isXmlHttpRequest()) { $url = $this->url()->fromRoute($routeName, $routeParams); $response = $this->redirect()->toUrl($url); $this->getResponse()->setStatusCode(301); return $response; } $router = $this->getServiceLocator()->get('Router'); $request = new Request(); $request->setMethod(Request::METHOD_GET); $request->setUri($url); $routeMatch = $router->match($request); if (!$routeMatch) { throw new RuntimeException(sprintf('Could not match a route for `%s`', $url)); } $params = array_merge($routeMatch->getParams(), ['forwarded' => true, 'isXmlHttpRequest' => true]); $controller = $params['controller']; $response = $this->forward()->dispatch($controller, $params); // TODO: Do me a favor and remove this piece of cr*p with something that doesn't hack the whole thing if ($response instanceof JsonModel) { $response = new ViewModel(['data' => $response->getVariables()]); $response->setTemplate('normalizer/json'); } $view = new ViewModel(['id' => $object->getId(), 'type' => $type, 'url' => $url, '__disableTemplateDebugger' => true]); $view->addChild($response, 'response'); $view->setTemplate('normalizer/ref'); $view->setTerminal(true); return $view; }
/** * @group view-model */ public function testIfViewModelComposesVariablesInstanceThenRendererUsesIt() { $model = new ViewModel(); $model->setTemplate('template'); $vars = $model->getVariables(); $vars['foo'] = 'BAR-BAZ-BAT'; $resolver = new TemplateMapResolver(array('template' => __DIR__ . '/_templates/view-model-variables.phtml')); $this->renderer->setResolver($resolver); $test = $this->renderer->render($model); $this->assertContains('BAR-BAZ-BAT', $test); }
public function testPropertyOverloadingAllowsWritingPropertiesAfterSetVariablesHasBeenCalled() { $model = new ViewModel(); $model->setVariables(array('foo' => 'bar')); $model->bar = 'baz'; $this->assertTrue(isset($model->bar)); $this->assertEquals('baz', $model->bar); $variables = $model->getVariables(); $this->assertTrue(isset($variables['bar'])); $this->assertEquals('baz', $variables['bar']); }
public function testPropertyOverloadingGivesAccessToProperties() { $model = new ViewModel(); $variables = $model->getVariables(); $model->foo = 'bar'; $this->assertTrue(isset($model->foo)); $this->assertEquals('bar', $variables['foo']); $this->assertEquals('bar', $model->foo); unset($model->foo); $this->assertFalse(isset($model->foo)); $this->assertFalse(isset($variables['foo'])); }
/** * Set strict vars on a view model recursively * * @param \Zend\View\Model\ViewModel $model */ protected function _setStrictVars(\Zend\View\Model\ViewModel $model) { $vars = $model->getVariables(); if (!$vars instanceof \Zend\View\Variables) { $vars = new \Zend\View\Variables($vars); } $vars->setStrictVars(true); $model->setVariables($vars, true); foreach ($model->getChildren() as $child) { $this->_setStrictVars($child); } }