本文整理汇总了PHP中Zend\Mvc\MvcEvent::setViewModel方法的典型用法代码示例。如果您正苦于以下问题:PHP MvcEvent::setViewModel方法的具体用法?PHP MvcEvent::setViewModel怎么用?PHP MvcEvent::setViewModel使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Zend\Mvc\MvcEvent
的用法示例。
在下文中一共展示了MvcEvent::setViewModel方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: runControllerAction
/**
* @param $controllerName
* @param $action
* @param array $params
* @return string|\Zend\Stdlib\ResponseInterface
* @throws \Exception
*/
public function runControllerAction($controllerName, $action, $params = array())
{
$this->event->getRouteMatch()->setParam('controller', $controllerName)->setParam('action', $action);
foreach ($params as $key => $value) {
$this->event->getRouteMatch()->setParam($key, $value);
}
$serviceManager = $this->event->getApplication()->getServiceManager();
$controllerManager = $serviceManager->get('ControllerLoader');
/** @var AbstractActionController $controller */
$controller = $controllerManager->get($controllerName);
$controller->setEvent($this->event);
$result = $controller->dispatch($this->event->getRequest());
if ($result instanceof Response) {
return $result;
}
/** @var ViewManager $viewManager */
$viewManager = $serviceManager->get('ViewManager');
$renderingStrategy = $viewManager->getMvcRenderingStrategy();
$this->event->setViewModel($result);
/** @var ViewModel $result */
if (!$result->terminate()) {
$layout = new ViewModel();
$layoutTemplate = $renderingStrategy->getLayoutTemplate();
$layout->setTemplate($layoutTemplate);
$layout->addChild($result);
$this->event->setViewModel($layout);
}
$response = $renderingStrategy->render($this->event);
return $response;
}
示例2: setUp
/**
* Set up
*/
public function setUp()
{
parent::setUp();
$this->resourceResponseListener = new ResourceResponseListener();
// Init the MvcEvent object
$this->response = new HttpResponse();
$this->event = new MvcEvent();
$this->event->setResponse($this->response);
$this->event->setViewModel(new ResourceViewModel());
}
示例3: testCollect
public function testCollect()
{
$event = new MvcEvent();
$layoutModel = new ViewModel();
$layoutModel->setTemplate('layout/2cols-left');
$event->setViewModel($layoutModel);
$testBlock = new ViewModel();
$testBlock->setTemplate('widget1');
$testBlock->setCaptureTo('sidebarLeft');
$testBlock2 = new ViewModel();
$testBlock2->setOption('parent', 'test.block');
$testBlock2->setTemplate('widget1');
$this->blockPool->add('test.block', $testBlock);
$this->blockPool->add('test.block2', $testBlock2);
$this->collector->collect($event);
$this->assertEquals('layout/2cols-left', $this->collector->getLayoutTemplate());
$this->assertInternalType('array', $this->collector->getHandles());
$this->assertContainsOnlyInstancesOf(HandleInterface::class, $this->collector->getHandles());
$this->assertInternalType('array', $this->collector->getBlocks());
$blocks = $this->collector->getBlocks();
$testBlockArray = current($blocks);
$testBlock2Array = array_pop($blocks);
$this->assertEquals('test.block::content', $testBlock2Array['capture_to']);
$this->assertContains('_files/view/widget1.phtml', $testBlockArray['template']);
$this->assertEquals('sidebarLeft', $testBlockArray['capture_to']);
$this->assertEquals(ViewModel::class, $testBlockArray['class']);
$this->assertEquals(LayoutUpdaterInterface::AREA_DEFAULT, $this->collector->getCurrentArea());
$this->assertInternalType('array', $this->collector->getLayoutStructure());
}
示例4: onDispatch
/**
* @param MvcEvent $event
*
* @return parent::onDispatch
*/
public function onDispatch(MvcEvent $event)
{
$request = $event->getRequest();
$remoteAddr = $request->getServer('REMOTE_ADDR');
// check IP address is allowed
$application = $event->getApplication();
$config = $application->getConfig();
$autoDeployConfig = $config['auto_deploy'];
$allowedIpAddresses = $autoDeployConfig['ipAddresses'];
// error if ip is not allowed
if (!in_array($remoteAddr, $allowedIpAddresses, true)) {
$baseModel = new \Zend\View\Model\ViewModel();
$baseModel->setTemplate('layout/output');
$model = new \Zend\View\Model\ViewModel();
$model->setTemplate('error/403');
$baseModel->addChild($model);
$baseModel->setTerminal(true);
$event->setViewModel($baseModel);
$response = $event->getResponse();
$response->setStatusCode(403);
$response->sendHeaders();
$event->setResponse($response);
exit;
}
return parent::onDispatch($event);
}
示例5: testOnDispatch
public function testOnDispatch()
{
// Create MvcEvent
$e = new MvcEvent();
$e->setViewModel(new ViewModel());
$rm = new RouteMatch([]);
$rm->setParam('controller', 'Application\\Controller\\Download');
$e->setRouteMatch($rm);
$e->setTarget(new DownloadController([]));
// Create EntityManager and EntityRepository
$moduleDetail = new ModuleList();
$moduleDetail->setModuleDesc('Pretty description');
$repo = $this->prophesize('Doctrine\\ORM\\EntityRepository');
$repo->findOneBy(['moduleName' => 'Application'])->willReturn($moduleDetail);
$em = $this->prophesize('Doctrine\\ORM\\EntityManager');
$em->getRepository('Application\\Entity\\ModuleList')->willReturn($repo);
$this->sm->setService('Doctrine\\ORM\\EntityManager', $em->reveal());
// Create ViewHelperManager
$headTitle = new HeadTitle();
$vhm = new HelperPluginManager();
$vhm->setService('headTitle', $headTitle);
$this->sm->setService('ViewHelperManager', $vhm);
$this->module->onDispatch($e);
$fbMeta = $e->getViewModel()->getVariable('fbMeta');
$this->assertEquals(sprintf('%s-Real Live Learn ZF2', $moduleDetail->getModuleDesc()), $fbMeta['title']);
$this->assertEquals(sprintf('%s-', $moduleDetail->getModuleDesc()), $fbMeta['description']);
}
示例6: testInjectTagsHeader
public function testInjectTagsHeader()
{
$tag = InjectTagsHeaderListener::OPTION_CACHE_TAGS;
$event = new MvcEvent();
$response = new Response();
$event->setResponse($response);
$layout = new ViewModel();
$child1 = new ViewModel();
$child1->setOption($tag, ['tag1', 'tag2']);
$layout->addChild($child1);
$child2 = new ViewModel();
$child21 = new ViewModel();
$child21->setOption($tag, ['tag3', null]);
$child2->addChild($child21);
$layout->addChild($child2);
$child3 = new ViewModel();
$child3->setOption('esi', ['ttl' => 120]);
$child3->setOption($tag, 'tag4');
$layout->addChild($child3);
$event->setViewModel($layout);
$this->listener->injectTagsHeader($event);
$this->assertSame(['tag1', 'tag2', 'tag3'], $this->listener->getCacheTags());
$headers = $response->getHeaders();
$this->assertEquals('tag1,tag2,tag3', $headers->get(VarnishService::VARNISH_HEADER_TAGS)->getFieldValue());
}
示例7: onInvokation
protected function onInvokation(MvcEvent $e, $error = false)
{
$viewModel = $e->getResult();
$isJsonModel = $viewModel instanceof JsonModel;
$routeMatch = $e->getRouteMatch();
if ($routeMatch && $routeMatch->getParam('forceJson', false) || $isJsonModel || "json" == $e->getRequest()->getQuery('format') || "json" == $e->getRequest()->getPost('format')) {
if (!$isJsonModel) {
$model = new JsonModel();
if ($error) {
$model->status = 'error';
$model->message = $viewModel->message;
if ($viewModel->display_exceptions) {
if (isset($viewModel->exception)) {
$model->exception = $viewModel->exception->getMessage();
}
}
} else {
$model->setVariables($viewModel->getVariables());
}
$viewModel = $model;
$e->setResult($model);
$e->setViewModel($model);
}
$viewModel->setTerminal(true);
$strategy = new \Zend\View\Strategy\JsonStrategy(new \Zend\View\Renderer\JsonRenderer());
$view = $e->getApplication()->getServiceManager()->get('ViewManager')->getView();
$view->addRenderingStrategy(array($strategy, 'selectRenderer'), 10);
$view->addResponseStrategy(array($strategy, 'injectResponse'), 10);
}
}
示例8: injectViewModel
/**
* Insert the view model into the event
*
* Inspects the MVC result; if it's a view model, it then either (a) adds
* it as a child to the default, composed view model, or (b) replaces it
* if the result is marked as terminable.
*
* @param MvcEvent $e
* @return void
*/
public function injectViewModel(MvcEvent $e)
{
$result = $e->getResult();
if (!$result instanceof ViewModel) {
return;
}
$model = $e->getViewModel();
if ($result->terminate()) {
$e->setViewModel($result);
return;
}
$model->addChild($result);
}
示例9: controlarAcces
private function controlarAcces(MvcEvent $e)
{
if (get_class($e->getTarget()) != 'LlistaCompra\\Controller\\SeguretatController') {
if (!UsuariConnectat::estaConnectat()) {
$res = [];
$res[0] = new RespostaTO();
$res[0]->resultat = "NC";
$model = new JsonModel($res);
$e->setViewModel($model);
$e->stopPropagation();
return $model;
}
}
}
示例10: onRender
/**
* Method executed when the render event is triggered
*
* @param MvcEvent $e
* @return void
*/
public static function onRender(MvcEvent $e)
{
if ($e->getRequest() instanceof \Zend\Console\Request || $e->getResponse()->isOk() || $e->getResponse()->getStatusCode() == Response::STATUS_CODE_401) {
return;
}
$httpCode = $e->getResponse()->getStatusCode();
$sm = $e->getApplication()->getServiceManager();
$viewModel = $e->getResult();
$exception = $viewModel->getVariable('exception');
$model = new JsonModel(array('errorCode' => !empty($exception) ? $exception->getCode() : $httpCode, 'errorMsg' => !empty($exception) ? $exception->getMessage() : NULL));
$model->setTerminal(true);
$e->setResult($model);
$e->setViewModel($model);
$e->getResponse()->setStatusCode($httpCode);
}
示例11: triggerForbiddenEvent
/**
*
* @param MvcEvent $event
*/
protected function triggerForbiddenEvent(MvcEvent $event)
{
$event->setError('route-forbidden');
$event->setParam('exception', new UnauthorizedException('You are forbidden!', 403));
$event->stopPropagation(true);
if ($this->hasErrorViewModel()) {
$event->setViewModel($this->errorView);
}
$response = $event->getResponse();
$response->setStatusCode(403);
$event->setResponse($response);
$application = $event->getApplication();
$eventManager = $application->getEventManager();
$eventManager->trigger(MvcEvent::EVENT_DISPATCH_ERROR, $event);
}
示例12: injectViewModel
/**
* Insert the view model into the event
*
* Inspects the MVC result; if it's a view model, it then either (a) adds
* it as a child to the default, composed view model, or (b) replaces it
* if the result is marked as terminable.
*
* @param MvcEvent $e
* @return void
*/
public function injectViewModel(MvcEvent $e)
{
$result = $e->getResult();
if (!$result instanceof ViewModel) {
return;
}
$model = $e->getViewModel();
if ($result->terminate()) {
$e->setViewModel($result);
return;
}
if ($e->getError() && $model instanceof ClearableModelInterface) {
$model->clearChildren();
}
$model->addChild($result);
}
示例13: injectLayout
public function injectLayout(MvcEvent $e)
{
$request = $e->getRequest();
if (!$request instanceof HttpRequest) {
return;
}
$result = $e->getResult();
if (!$result instanceof ZendViewModel) {
return;
}
$response = $e->getResponse();
if ($response->getStatusCode() != 200) {
return;
}
$matchedRoute = $e->getRouteMatch();
if (!$matchedRoute) {
return;
}
$matchedRouteName = $matchedRoute->getMatchedRouteName();
$serviceLocator = $e->getApplication()->getServiceManager();
$config = $serviceLocator->get('config');
$options = $config['sebaks-view'];
if (!isset($options['contents'][$matchedRouteName])) {
return;
}
$viewConfig = $options['contents'][$matchedRouteName];
$config = new Config(array_merge($options['layouts'], $options['contents'], $options['blocks']));
$viewConfig = $config->applyInheritance($viewConfig);
$viewBuilder = new ViewBuilder($config, $serviceLocator);
$data = $result->getVariables()->getArrayCopy();
if (isset($viewConfig['layout'])) {
$viewComponentLayout = $viewConfig['layout'];
if (!isset($options['layouts'][$viewComponentLayout])) {
throw new \Exception("Layout '{$viewComponentLayout}' not found for view component '{$matchedRouteName}'");
}
$options['layouts'][$viewComponentLayout]['children']['content'] = $viewConfig;
$viewComponent = $viewBuilder->build($options['layouts'][$viewComponentLayout], $data);
} else {
$viewComponent = $viewBuilder->build($viewConfig, $data);
}
$viewComponent->setTerminal(true);
$e->setViewModel($viewComponent);
$e->setResult($viewComponent);
}
示例14: onRender
public function onRender(MvcEvent $e)
{
if ($e->getResponse()->isOk()) {
return;
}
$httpCode = $e->getResponse()->getStatusCode();
$sm = $e->getApplication()->getServiceManager();
$viewModel = $e->getViewModel();
$exception = $viewModel->getVariable('exception');
$model = new JsonModel();
if ($exception) {
$model->setVariables(['errorCode' => $exception->getCode, 'errorMessage' => $exception->getMessage()]);
} else {
$model->setVariables(['errorCode' => $httpCode, 'errorMessage' => 'error has been trigger']);
}
$e->setResult($model);
$e->setViewModel($model);
$e->getResponse()->setStatusCode($httpCode);
}
示例15: testCollect
public function testCollect()
{
$event = new MvcEvent();
$layoutModel = new ViewModel();
$layoutModel->setTemplate('layout/2cols-left');
$event->setViewModel($layoutModel);
$testBlock = new ViewModel();
$testBlock->setTemplate('test/block');
$testBlock->setCaptureTo('sidebarLeft');
$this->layout->addBlock('test.block', $testBlock);
$this->collector->collect($event);
$this->assertEquals('layout/2cols-left', $this->collector->getLayoutTemplate());
$this->assertInternalType('array', $this->collector->getHandles());
$this->assertContainsOnlyInstancesOf('ConLayout\\Handle\\HandleInterface', $this->collector->getHandles());
$this->assertInternalType('array', $this->collector->getBlocks());
$testBlockArray = current($this->collector->getBlocks());
$this->assertEquals('test/block', $testBlockArray['template']);
$this->assertEquals('sidebarLeft', $testBlockArray['capture_to']);
$this->assertEquals('Zend\\View\\Model\\ViewModel', $testBlockArray['class']);
$this->assertInternalType('array', $this->collector->getLayoutStructure());
}