Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?php
namespace MyApp\Controller;
use Zend\Mvc\Controller\AbstractActionController;
use \Zend\View\Model\ViewModel;
use Zend\View\Model\JsonModel;
class IndexController extends AbstractActionController
{
/**
*
* @var \Zend\View\HelperPluginManager
*/
private $viewHelperManager;
/**
*
* @var \Zend\View\Renderer\RendererInterface
*/
protected $viewRenderer;
public function indexAction()
{
$jsonModel = new JsonModel();
/*
* generate HTML through ViewModel
* and render them
*/
$viewModel = new ViewModel();
$viewModel->setTemplate('template/panel/some-content');
$html1 = $this->viewRender->render($viewModel);
$jsonModel->setVariable('html1', $html1);
$this->logger->debug(json_encode($html1));
/*
* generate HTML through ViewHelper
* and render them
*/
$this->myViewHelper = $this->viewHelperManager->get('myViewHelper');
$html2 = call_user_func($this->myViewHelper);
$jsonModel->setVariable('html2', $html2);
$this->logger->debug(json_encode($html2));
/*
* think about ViewJsonStrategy in config view_manager strategies
* to send it without view file
*/
return $jsonModel;
}
/**
*
* @param \Zend\View\HelperPluginManager $viewHelperManager
*/
public function setViewHelperManager(\Zend\View\HelperPluginManager $viewHelperManager)
{
$this->viewHelperManager = $viewHelperManager;
}
/**
*
* @param \Zend\View\Renderer\RendererInterface $viewRenderer
*/
public function setViewRenderer(\Zend\View\Renderer\RendererInterface $viewRenderer)
{
$this->viewRenderer = $viewRenderer;
}
}
namespace MyApp\Factory\Controller;
use Zend\ServiceManager\FactoryInterface;
class IndexControllerFactory implements FactoryInterface
{
public function createService(\Zend\ServiceManager\ServiceLocatorInterface $sli)
{
$ctr = new \MyApp\Controller\IndexController();
$sl = $sli->getServiceLocator();
$ctr->setLogger($sl->get('logger'));
$ctr->setViewHelperManager($sl->get('ViewHelperManager'));
$ctr->setViewRenderer($sl->get('ViewRenderer'));
return $ctr;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.