|
| 1 | +# Advanced usage of helpers |
| 2 | + |
| 3 | +## Registering Helpers |
| 4 | + |
| 5 | +`Zend\View\Renderer\PhpRenderer` composes a *plugin manager* for managing |
| 6 | +helpers, specifically an instance of `Zend\View\HelperPluginManager`, which |
| 7 | +extends `Zend\ServiceManager\AbstractPluginManager`, which is itself an |
| 8 | +extension of `Zend\ServiceManager\ServiceManager`. `HelperPluginManager` is a |
| 9 | +specialized service manager, so you can register a helper/plugin like any other |
| 10 | +service (see the [Service Manager documentation](http://zendframework.github.io/zend-servicemanager/configuring-the-service-manager/) |
| 11 | +for more information). |
| 12 | + |
| 13 | +Programmatically, this is done as follows: |
| 14 | + |
| 15 | +```php |
| 16 | +use MyModule\View\Helper\LowerCase; |
| 17 | + |
| 18 | +// $view is an instance of PhpRenderer |
| 19 | +$pluginManager = $view->getHelperPluginManager(); |
| 20 | + |
| 21 | +// Register an alias: |
| 22 | +$pluginManager->setAlias('lowercase', LowerCase::class); |
| 23 | + |
| 24 | +// Register a factory: |
| 25 | +$pluginManager->setFactory(LowerCase::class, function () { |
| 26 | + $lowercaseHelper = new LowerCase(); |
| 27 | + |
| 28 | + // ...do some configuration or dependency injection... |
| 29 | + |
| 30 | + return $lowercaseHelper; |
| 31 | +}); |
| 32 | +``` |
| 33 | + |
| 34 | +Within an MVC application, you will typically pass a map of plugins to the class |
| 35 | +via your configuration. |
| 36 | + |
| 37 | +```php |
| 38 | +use MyModule\View\Helper; |
| 39 | +use Zend\ServiceManager\Factory\InvokableFactory; |
| 40 | + |
| 41 | +// From within a configuration file |
| 42 | +return [ |
| 43 | + 'view_helpers' => [ |
| 44 | + 'aliases' => [ |
| 45 | + 'lowercase' => Helper\LowerCase::class, |
| 46 | + 'uppercase' => Helper\UpperCase::class, |
| 47 | + ], |
| 48 | + 'factories' => [ |
| 49 | + LowerCase::class => InvokableFactory::class, |
| 50 | + UpperCase::class => InvokableFactory::class, |
| 51 | + ], |
| 52 | + ], |
| 53 | +]; |
| 54 | +``` |
| 55 | + |
| 56 | +If your module class implements `Zend\ModuleManager\Feature\ViewHelperProviderInterface`, |
| 57 | +or just the method `getViewHelperConfig()`, you could also do the following |
| 58 | +(it's the same as the previous example). |
| 59 | + |
| 60 | +```php |
| 61 | +namespace MyModule; |
| 62 | + |
| 63 | +class Module |
| 64 | +{ |
| 65 | + public function getViewHelperConfig() |
| 66 | + { |
| 67 | + return [ |
| 68 | + 'aliases' => [ |
| 69 | + 'lowercase' => Helper\LowerCase::class, |
| 70 | + 'uppercase' => Helper\UpperCase::class, |
| 71 | + ], |
| 72 | + 'factories' => [ |
| 73 | + LowerCase::class => InvokableFactory::class, |
| 74 | + UpperCase::class => InvokableFactory::class, |
| 75 | + ], |
| 76 | + ]; |
| 77 | + } |
| 78 | +} |
| 79 | +``` |
| 80 | + |
| 81 | +The two latter examples can be done in each module that needs to register |
| 82 | +helpers with the `PhpRenderer`; however, be aware that another module can |
| 83 | +register helpers with the same name, so order of modules can impact which helper |
| 84 | +class will actually be registered! |
| 85 | + |
| 86 | +## Writing Custom Helpers |
| 87 | + |
| 88 | +Writing custom helpers is easy. We recommend extending |
| 89 | +`Zend\View\Helper\AbstractHelper`, but at the minimum, you need only implement |
| 90 | +the `Zend\View\Helper\HelperInterface` interface: |
| 91 | + |
| 92 | +```php |
| 93 | +namespace Zend\View\Helper; |
| 94 | + |
| 95 | +use Zend\View\Renderer\RendererInterface as Renderer; |
| 96 | + |
| 97 | +interface HelperInterface |
| 98 | +{ |
| 99 | + /** |
| 100 | + * Set the View object |
| 101 | + * |
| 102 | + * @param Renderer $view |
| 103 | + * @return HelperInterface |
| 104 | + */ |
| 105 | + public function setView(Renderer $view); |
| 106 | + |
| 107 | + /** |
| 108 | + * Get the View object |
| 109 | + * |
| 110 | + * @return Renderer |
| 111 | + */ |
| 112 | + public function getView(); |
| 113 | +} |
| 114 | +``` |
| 115 | + |
| 116 | +If you want your helper to be capable of being invoked as if it were a method call of the |
| 117 | +`PhpRenderer`, you should also implement an `__invoke()` method within your helper. |
| 118 | + |
| 119 | +As previously noted, we recommend extending `Zend\View\Helper\AbstractHelper`, as it implements the |
| 120 | +methods defined in `HelperInterface`, giving you a headstart in your development. |
| 121 | + |
| 122 | +> ### Invokable helpers |
| 123 | +> |
| 124 | +> Starting with version 2.7.0, helpers no longer need to be instances of |
| 125 | +> `HelperInterface`, but can be *any* PHP callable. We recommend writing helpers |
| 126 | +> as invokable classes (classes implementing `__invoke()`. |
| 127 | +
|
| 128 | +Once you have defined your helper class, make sure you can autoload it, and then |
| 129 | +register it with the [plugin manager](#registering-helpers). |
| 130 | + |
| 131 | +Here is an example helper, which we're titling "SpecialPurpose" |
| 132 | + |
| 133 | +```php |
| 134 | +namespace MyModule\View\Helper; |
| 135 | + |
| 136 | +use Zend\View\Helper\AbstractHelper; |
| 137 | + |
| 138 | +class SpecialPurpose extends AbstractHelper |
| 139 | +{ |
| 140 | + protected $count = 0; |
| 141 | + |
| 142 | + public function __invoke() |
| 143 | + { |
| 144 | + $this->count++; |
| 145 | + $output = sprintf("I have seen 'The Jerk' %d time(s).", $this->count); |
| 146 | + return htmlspecialchars($output, ENT_QUOTES, 'UTF-8'); |
| 147 | + } |
| 148 | +} |
| 149 | +``` |
| 150 | + |
| 151 | +Then assume that we [register it with the plugin manager](#registering-helpers) |
| 152 | +by the name "specialpurpose". |
| 153 | + |
| 154 | +Within a view script, you can call the `SpecialPurpose` helper as many times as |
| 155 | +you like; it will be instantiated once, and then it persists for the life of |
| 156 | +that `PhpRenderer` instance. |
| 157 | + |
| 158 | +```php |
| 159 | +// remember, in a view script, $this refers to the Zend\View\Renderer\PhpRenderer instance. |
| 160 | +echo $this->specialPurpose(); |
| 161 | +echo $this->specialPurpose(); |
| 162 | +echo $this->specialPurpose(); |
| 163 | +``` |
| 164 | + |
| 165 | +The output would look something like this: |
| 166 | + |
| 167 | +```php |
| 168 | +I have seen 'The Jerk' 1 time(s). |
| 169 | +I have seen 'The Jerk' 2 time(s). |
| 170 | +I have seen 'The Jerk' 3 time(s). |
| 171 | +``` |
| 172 | + |
| 173 | +Sometimes you will need access to the calling `PhpRenderer` object; for |
| 174 | +instance, if you need to use the registered encoding, or want to render another |
| 175 | +view script as part of your helper. This is why we define the `setView()` and |
| 176 | +`getView()` methods. As an example, we could rewrite the `SpecialPurpose` helper |
| 177 | +as follows to take advantage of the `EscapeHtml` helper: |
| 178 | + |
| 179 | +```php |
| 180 | +namespace MyModule\View\Helper; |
| 181 | + |
| 182 | +use Zend\View\Helper\AbstractHelper; |
| 183 | + |
| 184 | +class SpecialPurpose extends AbstractHelper |
| 185 | +{ |
| 186 | + protected $count = 0; |
| 187 | + |
| 188 | + public function __invoke() |
| 189 | + { |
| 190 | + $this->count++; |
| 191 | + $output = sprintf("I have seen 'The Jerk' %d time(s).", $this->count); |
| 192 | + $escaper = $this->getView()->plugin('escapehtml'); |
| 193 | + return $escaper($output); |
| 194 | + } |
| 195 | +} |
| 196 | +``` |
| 197 | + |
| 198 | +> ### Accessing the view or other helpers in callables |
| 199 | +> |
| 200 | +> As noted earlier, starting in version 2.7.0, you may use any PHP callable as a |
| 201 | +> helper. If you do, however, how can you access the renderer or other plugins? |
| 202 | +> |
| 203 | +> The answer is: dependency injection. |
| 204 | +> |
| 205 | +> If you write your helper as a class, you can accept dependencies via the |
| 206 | +> constructor or other setter methods. Create a factory that pulls those |
| 207 | +> dependencies and injects them. |
| 208 | +> |
| 209 | +> As an example, if we need the `escapeHtml()` helper, we could write our helper |
| 210 | +> as follows: |
| 211 | +> |
| 212 | +> ```php |
| 213 | +> namespace MyModule\View\Helper; |
| 214 | +> |
| 215 | +> use Zend\View\Helper\EscapeHtml; |
| 216 | +> |
| 217 | +> class SpecialPurpose |
| 218 | +> { |
| 219 | +> private $count = 0; |
| 220 | +> |
| 221 | +> private $escaper; |
| 222 | +> |
| 223 | +> public function __construct(EscapeHtml $escaper) |
| 224 | +> { |
| 225 | +> $this->escaper = $escaper; |
| 226 | +> } |
| 227 | +> |
| 228 | +> public function __invoke() |
| 229 | +> { |
| 230 | +> $this->count++; |
| 231 | +> $output = sprintf("I have seen 'The Jerk' %d time(s).", $this->count); |
| 232 | +> $escaper = $this->escaper; |
| 233 | +> return $escaper($output); |
| 234 | +> } |
| 235 | +> } |
| 236 | +> ``` |
| 237 | +> |
| 238 | +> Then we would write a factory like the following: |
| 239 | +> |
| 240 | +> ```php |
| 241 | +> use Zend\ServiceManager\AbstractPluginManager; |
| 242 | +> |
| 243 | +> class SpecialPurposeFactory |
| 244 | +> { |
| 245 | +> public function __invoke($container) |
| 246 | +> { |
| 247 | +> if (! $container instanceof AbstractPluginManager) { |
| 248 | +> // zend-servicemanager v3. v2 passes the helper manager directly. |
| 249 | +> $container = $container->get('ViewHelperManager'); |
| 250 | +> } |
| 251 | +> |
| 252 | +> return new SpecialPurpose($container->get('escapeHtml')); |
| 253 | +> } |
| 254 | +> } |
| 255 | +> ``` |
| 256 | +> |
| 257 | +> If access to the view were required, we'd pass the `PhpRenderer` service |
| 258 | +> instead. |
| 259 | +
|
| 260 | +## Registering Concrete Helpers |
| 261 | +
|
| 262 | +Sometimes it is convenient to instantiate a view helper, and then register it |
| 263 | +with the renderer. This can be done by injecting it directly into the plugin |
| 264 | +manager. |
| 265 | +
|
| 266 | +```php |
| 267 | +// $view is a PhpRenderer instance |
| 268 | +
|
| 269 | +$helper = new MyModule\View\Helper\LowerCase; |
| 270 | +// ...do some configuration or dependency injection... |
| 271 | +
|
| 272 | +$view->getHelperPluginManager()->setService('lowercase', $helper); |
| 273 | +``` |
| 274 | +
|
| 275 | +The plugin manager will validate the helper/plugin, and if the validation |
| 276 | +passes, the helper/plugin will be registered. |
0 commit comments