Helpers

Introduction

In your view scripts, you'll perform certain complex functions over and over: e.g., formatting a date, generating form elements, or displaying action links. You can use helpers, or plugins to perform these behaviors for you.

A helper is a class that will normally have an __invoke() method as the entry point to perform its function. For further details on creating your own custom helpers, take a look at the advanced usage chapter.

Helpers are retrieved from the HelperPluginManager by their fully qualified class name or alias. It is the registered alias that we use inside templates to call view helpers.

Fetching or Using Helpers from Within Templates

As a recap from the templates and view scripts documentation, we call helpers by using their alias like a method name on the template instance:

// some-template.phtml
?>
<h1>Hi there <?= $this->escapeHtml($this->name) ?></h1>

In the above example, the EscapeHtml helper is retrieved from the plugin manager, its __invoke method is called with the value to be escaped, and it returns the escaped string for output in the rendered markup.

Accessing View Helpers Outside the Rendering Cycle

The HelperPluginManager should be available in your applications dependency injection container. To retrieve plugin instances, you must retrieve the plugin manager and then retrieve the helper from the plugin manager:

use Laminas\View\Helper\EscapeHtml;
use Laminas\View\HelperPluginManager;
use Psr\Container\ContainerInterface;

/** @var ContainerInterface $container */
$plugins = $container->get(HelperPluginManager::class);
$escaper = $plugins->get(EscapeHtml::class);
$value = $escaper($someValueToEscape);

Typically, Mezzio applications, or those that largely make use of Laminas components will use Laminas Service Manager for dependency injection. However, your main DI container does not need to be a ServiceManager instance in order to use laminas-view and its plugin system. Any PSR-11 container can be used, but it will be your responsibility to ensure services are wired up appropriately when Laminas Service Manager is not in use. The ConfigProvider can be inspected to determine how the various services are identified and created.

Example Manual ServiceManager Setup This example shows how to retrieve plugin instances in isolation without the benefits of using "Config Providers" to illustrate the steps that might be taken when using Laminas View with another PSR-11 DI container.
use Laminas\ServiceManager\ServiceManager;
use Laminas\View\Factory\HelperPluginManagerFactory;
use Laminas\View\Helper\HeadTitle;
use Laminas\View\HelperPluginManager;

// Our main application DI container
$container = new ServiceManager();
// Wire up the factory to create the plugin manager
$container->setFactory(HelperPluginManager::class, HelperPluginManagerFactory::class);
// Fetch a plugin manager instance
$pluginManager = $container->get(HelperPluginManager::class);
// Retrieve helpers
$headTitleHelper = $pluginManager->get(HeadTitle::class);
// Do things with the helper…
$headTitleHelper->setPrefix('Title: ');

Included Helpers

Laminas View comes with a number of helpers for easing the creation of markup in web applications.

The currently shipped helpers include:

i18n Helpers

View helpers related to Internationalization are documented in the I18n View Helpers documentation.

Form Helpers

View helpers related to form are documented in the Form View Helpers documentation.

Navigation Helpers

View helpers related to navigation are documented in the Navigation View Helpers documentation.

Pagination Helpers

View helpers related to paginator are documented in the Paginator Usage documentation.

FlashMessenger Helper

View helper related to Flash Messenger is documented in the FLash Messenger View Helper documentation.

Custom Helpers

For documentation on writing custom view helpers see the Advanced usage chapter.