laminas/laminas-view
Laminas View provides a flexible view layer for PHP applications, offering template rendering, view models, helpers, and multiple rendering strategies. Integrates with Laminas MVC and works standalone to manage layouts, partials, and output generation.
Start by installing via Composer: composer require laminas/laminas-view. In a Laminas MVC application, it's already wired in via the full skeleton, but for custom setups (e.g., Expressive/Mezzio or standalone), bind the renderer and view manager manually. Begin with the simplest use case: render a PHP template with a ViewModel:
// In a controller action
use Laminas\View\Model\ViewModel;
public function indexAction()
{
$viewModel = new ViewModel(['title' => 'Hello World']);
$viewModel->setTemplate('layout/main'); // optional
return $viewModel;
}
Then create view/layout/main.phtml and view/application/index/index.phtml. The default renderer is Laminas\View\Renderer\PhpRenderer, and templates are resolved automatically using configured view paths. Check config/autoload/global.php for view_manager.template_path_stack.
ViewModel::setParent() or ViewModel::addChild() to nest view models for reusable page structures (e.g., main layout → sidebar → content block).ViewModel::setTerminal(true) + laminas/laminas-json-parser for JSON APIs, or use JsonModel (if laminas/laminas-mvc is present).baseUrl(), escapeHtml(), partial()) in templates. Register custom helpers via plugin manager in configuration:
'view_helpers' => [
'invokables' => [
'myHelper' => My\Helper::class,
],
];
escapeHtml(), escapeJs(), escapeCss(), escapeUrl() explicitly—don’t assume automatic escaping in templates.laminas/laminas-view-strategy-twig or custom renderer, but prefer PhpRenderer for tight Laminas integration.config/cache.php has view_manager.cache_dir enabled—clear cache manually or disable in dev.viewHelperManager or getServiceManager()->get('ViewHelperManager') only inside templates.ViewModel::$template or use JsonModel/TerminalViewModel to avoid HTML rendering when JSON is intended.Renderer\PhpRenderer and mocked view model—no HTTP stack needed:
$renderer = new PhpRenderer();
$renderer->setResolver(new Resolver\TemplateMapResolver(['test' => __DIR__ . '/test.phtml']));
echo $renderer->render(new ViewModel(['var' => 'test']), 'test');
How can I help you explore Laravel packages today?