Alors, je propose de créer un nouveau plugin 'htmlRender':
module.config.php
'controller_plugins' => [
'factories' => [
'htmlRender' => Application\Mvc\Controller\Plugin\Service\HtmlRenderFactory::class,
],
],
HtmlRenderFactory.php
<?php
namespace Application\Mvc\Controller\Plugin\Service;
use Application\Mvc\Controller\Plugin\HtmlRender;
use Interop\Container\ContainerInterface;
use Zend\ServiceManager\Factory\FactoryInterface;
use Zend\View\Renderer\PhpRenderer;
class HtmlRenderFactory implements FactoryInterface
{
public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
{
$plugin = new HtmlRender();
$plugin->setRenderer($container->get(PhpRenderer::class));
return $plugin;
}
}
HtmlRender.php
<?php
namespace Application\Mvc\Controller\Plugin;
use Zend\Mvc\Controller\Plugin\AbstractPlugin;
use Zend\View\Renderer\RendererInterface;
class HtmlRender extends AbstractPlugin
{
/**
* @var \Zend\View\Renderer\PhpRenderer
*/
protected $renderer;
/**
* @param string|\Zend\View\Model\ModelInterface $nameOrModel
* @param null|array|\Traversable $values
* @param string|bool|\Zend\View\Model\ModelInterface $layout
* @return string
*/
public function __invoke($nameOrModel, $values = null, $layout = false)
{
$content = $this->getRenderer()->render($nameOrModel, $values);
if (!$layout) {
return $content;
}
if (true === $layout) {
$layout = 'layout/layout';
}
return $this->getRenderer()->render($layout, [
'content' => $content,
]);
}
/**
* @return \Zend\View\Renderer\PhpRenderer
*/
public function getRenderer()
{
return $this->renderer;
}
/**
* @param \Zend\View\Renderer\PhpRenderer|RendererInterface $renderer
* @return self
*/
public function setRenderer(RendererInterface $renderer)
{
$this->renderer = $renderer;
return $this;
}
}
Utilisation dans Mycontrolle rController.php
<?php
class MycontrollerController extends AbstractActionController
{
public function firstAction()
{
if ($this->yourMethod()) {
// Option 1 without layout
$html = $this->htmlRender($secondView);
// Option 2 without layout
$html = $this->htmlRender('namespace/my-controller/second', $yourVariables));
// Option 1 with layout
$html = $this->htmlRender($secondView, null, true);
//$html = $this->htmlRender($secondView, null, 'layout/my-custom-layout');
// Option 2 with layout
$html = $this->htmlRender('namespace/my-controller/second', $yourVariables, true));
//$html = $this->htmlRender('namespace/my-controller/second', $yourVariables, 'layout/my-custom-layout');
}
$view = new ViewModel();
$view->setTemplate('namespace/my-controller/first');
return $view;
}
public function secondAction()
{
$view = new ViewModel();
$view->setTemplate('namespace/my-controller/second');
return $view;
}
}
Cela a été utile, mais j'ai encore besoin de plus d'aide. Disons que j'ai fait ceci '$ otherViewModel = $ this-> secondAction();', maintenant je veux obtenir le html rendu de ce modèle de vue, quelque chose comme '$ html = $ otherViewModel-> some_render_function()', comment faire cette? – evilReiko
J'ai modifié ma réponse ;-) –
OMG c'est parfait! : D – evilReiko