Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Xerkus/a8c6fa3b622632c018ae4986d8c11ba4 to your computer and use it in GitHub Desktop.
Save Xerkus/a8c6fa3b622632c018ae4986d8c11ba4 to your computer and use it in GitHub Desktop.
Extracted ZF2 view registartion for use in silex
<?php
$app = new \Silex\Application();
$app->register(new \VendorNs\Silex\Provider\ViewServiceProvider(), [
'view.options' => ['template_path_stack' => __DIR__ . '/view'],
]);
$app->get('/about', function(\Silex\Application $app) {
return $app['view']->render('app/about', null, 'app/layout');
})
->bind('about');
<?php
namespace VendorNs\Silex\Provider;
use VendorNs\Silex\View;
use Silex\Application;
use Silex\ServiceProviderInterface;
use Zend\Form\View\HelperConfig as FormHelperConfig;
use Zend\View\Helper\FlashMessenger;
use Zend\View\Resolver as ViewResolver;
class ViewServiceProvider implements ServiceProviderInterface
{
public function register(Application $app)
{
$app['view.options'] = array();
$app['view.templatemap'] = $app->share(function ($app) {
$map = isset($app['view.options']['template_map'])
? (array) $app['view.options']['template_map']
: array();
return new ViewResolver\TemplateMapResolver($map);
});
$app['view.templatepathstack'] = $app->share(function ($app) {
$templatePathStack = new ViewResolver\TemplatePathStack();
if (isset($app['view.options']['template_path_stack'])) {
$templatePathStack->addPaths(
(array)$app['view.options']['template_path_stack']
);
}
return $templatePathStack;
});
$app['view'] = $app->share(function ($app) {
$resolver = new ViewResolver\AggregateResolver();
$resolver->attach($app['view.templatemap']);
$resolver->attach($app['view.templatepathstack']);
$renderer = new View($app);
$renderer->setResolver($resolver);
$renderer->plugin('basePath')->setBasePath($app['request']->getBasePath());
if (class_exists(FormHelperConfig::class, true)) {
$helperConfig = new FormHelperConfig;
$helperConfig->configureServiceManager($renderer->getHelperPluginManager());
}
// Override. Helper factory depends on controller plugin factory.
$renderer->getHelperPluginManager()->setService(
'flashMessenger',
$app['view.flash_messenger']
);
return $renderer;
});
$app['flash_messenger'] = $app->share(function ($app) {
return $app['view']->flashMessenger()->getPluginFlashMessenger();
});
$app['view.flash_messenger'] = $app->share(function ($app) {
$flash = new FlashMessenger;
$flash->setMessageOpenFormat(
'<div%s><button data-dismiss="alert" class="close" type="button">'
.'<i class="icon-remove"></i></button>'
)
->setMessageSeparatorString('</li><li>')
->setMessageCloseString('</li></ul></div>');
return $flash;
});
}
public function boot(Application $app)
{
}
}
<?php
namespace VendorNs\Silex;
use Zend\View\Renderer\PhpRenderer;
class View extends PhpRenderer
{
protected $app;
protected $__silexapp;
public function __construct($app)
{
$this->app = $this->__silexapp = $app;
}
public function render($nameOrModel, $values = null, $layout = null, $layoutValues = array())
{
$content = parent::render($nameOrModel, $values);
if (isset($layout)) {
return parent::render(
$layout,
array_merge($layoutValues, array('content' => $content))
);
}
return $content;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment