|
| 1 | +# Advanced usage of helpers |
| 2 | + |
| 3 | +## Registering Helpers |
| 4 | + |
| 5 | +`Zend\View\Renderer\PhpRenderer` composes a *plugin manager* for managing helpers, specifically an |
| 6 | +instance of `Zend\View\HelperPluginManager`, which extends |
| 7 | +`Zend\ServiceManager\AbstractPluginManager`, and this extends `Zend\ServiceManager\ServiceManager`. |
| 8 | +As you can see, the *HelperPluginManager* is a specialized service manager, so you can register a |
| 9 | +helper/plugin like any other service (see the Service Manager documentation |
| 10 | +<zend.service-manager.intro> for more information). |
| 11 | + |
| 12 | +Programmatically, this is done as follows: |
| 13 | + |
| 14 | +```php |
| 15 | +// $view is an instance of PhpRenderer |
| 16 | +$pluginManager = $view->getHelperPluginManager(); |
| 17 | + |
| 18 | +// Register as a invokable class: |
| 19 | +$pluginManager->setInvokableClass('lowercase', 'MyModule\View\Helper\LowerCase'); |
| 20 | + |
| 21 | +// Register as a factory: |
| 22 | +$pluginManager->setFactory('lowercase', function ($pluginManager) { |
| 23 | + $lowercaseHelper = new MyModule\View\Helper\LowerCase; |
| 24 | + |
| 25 | + // ...do some configuration or dependency injection... |
| 26 | + |
| 27 | + return $lowercaseHelper; |
| 28 | +}); |
| 29 | +``` |
| 30 | + |
| 31 | +Within an MVC application, you will typically simply pass a map of plugins to the class via your |
| 32 | +configuration. |
| 33 | + |
| 34 | +```php |
| 35 | +// From within a configuration file |
| 36 | +return array( |
| 37 | + 'view_helpers' => array( |
| 38 | + 'invokables' => array( |
| 39 | + 'lowercase' => 'MyModule\View\Helper\LowerCase', |
| 40 | + 'uppercase' => 'MyModule\View\Helper\UpperCase', |
| 41 | + ), |
| 42 | + ), |
| 43 | +); |
| 44 | +``` |
| 45 | + |
| 46 | +If your module class implements `Zend\ModuleManager\Feature\ViewHelperProviderInterface`, or just |
| 47 | +the method `getViewHelperConfig()`, you could do the following (it's the same as the previous |
| 48 | +example). |
| 49 | + |
| 50 | +```php |
| 51 | +namespace MyModule; |
| 52 | + |
| 53 | +class Module |
| 54 | +{ |
| 55 | + public function getAutoloaderConfig(){ /*common code*/ } |
| 56 | + public function getConfig(){ /*common code*/ } |
| 57 | + |
| 58 | + public function getViewHelperConfig() |
| 59 | + { |
| 60 | + return array( |
| 61 | + 'invokables' => array( |
| 62 | + 'lowercase' => 'MyModule\View\Helper\LowerCase', |
| 63 | + 'uppercase' => 'MyModule\View\Helper\UpperCase', |
| 64 | + ), |
| 65 | + ); |
| 66 | + } |
| 67 | +} |
| 68 | +``` |
| 69 | + |
| 70 | +The two latter examples can be done in each module that needs to register helpers with the |
| 71 | +`PhpRenderer`; however, be aware that another module can register helpers with the same name, so |
| 72 | +order of modules can impact which helper class will actually be registered! |
| 73 | + |
| 74 | +## Writing Custom Helpers |
| 75 | + |
| 76 | +Writing custom helpers is easy. We recommend extending `Zend\View\Helper\AbstractHelper`, but at the |
| 77 | +minimum, you need only implement the `Zend\View\Helper\HelperInterface` interface: |
| 78 | + |
| 79 | +```php |
| 80 | +namespace Zend\View\Helper; |
| 81 | + |
| 82 | +use Zend\View\Renderer\RendererInterface as Renderer; |
| 83 | + |
| 84 | +interface HelperInterface |
| 85 | +{ |
| 86 | + /** |
| 87 | + * Set the View object |
| 88 | + * |
| 89 | + * @param Renderer $view |
| 90 | + * @return HelperInterface |
| 91 | + */ |
| 92 | + public function setView(Renderer $view); |
| 93 | + |
| 94 | + /** |
| 95 | + * Get the View object |
| 96 | + * |
| 97 | + * @return Renderer |
| 98 | + */ |
| 99 | + public function getView(); |
| 100 | +} |
| 101 | +``` |
| 102 | + |
| 103 | +If you want your helper to be capable of being invoked as if it were a method call of the |
| 104 | +`PhpRenderer`, you should also implement an `__invoke()` method within your helper. |
| 105 | + |
| 106 | +As previously noted, we recommend extending `Zend\View\Helper\AbstractHelper`, as it implements the |
| 107 | +methods defined in `HelperInterface`, giving you a headstart in your development. |
| 108 | + |
| 109 | +Once you have defined your helper class, make sure you can autoload it, and then register it with |
| 110 | +the plugin |
| 111 | +manager <zend.view.helpers.register>. |
| 112 | + |
| 113 | +Here is an example helper, which we're titling "SpecialPurpose" |
| 114 | + |
| 115 | +```php |
| 116 | +// /module/src/MyModule/View/Helper/SpecialPurpose.php |
| 117 | +namespace MyModule\View\Helper; |
| 118 | + |
| 119 | +use Zend\View\Helper\AbstractHelper; |
| 120 | + |
| 121 | +class SpecialPurpose extends AbstractHelper |
| 122 | +{ |
| 123 | + protected $count = 0; |
| 124 | + |
| 125 | + public function __invoke() |
| 126 | + { |
| 127 | + $this->count++; |
| 128 | + $output = sprintf("I have seen 'The Jerk' %d time(s).", $this->count); |
| 129 | + return htmlspecialchars($output, ENT_QUOTES, 'UTF-8'); |
| 130 | + } |
| 131 | +} |
| 132 | +``` |
| 133 | + |
| 134 | +Then assume that we \[register it with the plugin manager\](zend.view.helpers.register), by the name |
| 135 | +"specialpurpose". |
| 136 | + |
| 137 | +Within a view script, you can call the `SpecialPurpose` helper as many times as you like; it will be |
| 138 | +instantiated once, and then it persists for the life of that `PhpRenderer` instance. |
| 139 | + |
| 140 | +```php |
| 141 | +// remember, in a view script, $this refers to the Zend\View\Renderer\PhpRenderer instance. |
| 142 | +echo $this->specialPurpose(); |
| 143 | +echo $this->specialPurpose(); |
| 144 | +echo $this->specialPurpose(); |
| 145 | +``` |
| 146 | + |
| 147 | +The output would look something like this: |
| 148 | + |
| 149 | +```php |
| 150 | +I have seen 'The Jerk' 1 time(s). |
| 151 | +I have seen 'The Jerk' 2 time(s). |
| 152 | +I have seen 'The Jerk' 3 time(s). |
| 153 | +``` |
| 154 | + |
| 155 | +Sometimes you will need access to the calling `PhpRenderer` object -- for instance, if you need to |
| 156 | +use the registered encoding, or want to render another view script as part of your helper. This is |
| 157 | +why we define the `setView()` and `getView()` methods. As an example, we could rewrite the |
| 158 | +`SpecialPurpose` helper as follows to take advantage of the `EscapeHtml` helper: |
| 159 | + |
| 160 | +```php |
| 161 | +namespace MyModule\View\Helper; |
| 162 | + |
| 163 | +use Zend\View\Helper\AbstractHelper; |
| 164 | + |
| 165 | +class SpecialPurpose extends AbstractHelper |
| 166 | +{ |
| 167 | + protected $count = 0; |
| 168 | + |
| 169 | + public function __invoke() |
| 170 | + { |
| 171 | + $this->count++; |
| 172 | + $output = sprintf("I have seen 'The Jerk' %d time(s).", $this->count); |
| 173 | + $escaper = $this->getView()->plugin('escapehtml'); |
| 174 | + return $escaper($output); |
| 175 | + } |
| 176 | +} |
| 177 | +``` |
| 178 | + |
| 179 | +## Registering Concrete Helpers |
| 180 | + |
| 181 | +Sometimes it is convenient to instantiate a view helper, and then register it with the renderer. |
| 182 | +This can be done by injecting it directly into the plugin manager. |
| 183 | + |
| 184 | +```php |
| 185 | +// $view is a PhpRenderer instance |
| 186 | + |
| 187 | +$helper = new MyModule\View\Helper\LowerCase; |
| 188 | +// ...do some configuration or dependency injection... |
| 189 | + |
| 190 | +$view->getHelperPluginManager()->setService('lowercase', $helper); |
| 191 | +``` |
| 192 | + |
| 193 | +The plugin manager will validate the helper/plugin, and if the validation passes, the helper/plugin |
| 194 | +will be registered. |
0 commit comments