elxordi
elxordi

Reputation: 486

How to add Helpers to a PhpRenderer in Zend Framework 2.0?

I am migrating my old application to the new Zend Framework 2.0. My app uses it just as a library (no Zend\Application use or anything of the MVC part), and I have problems using the Form Helpers. So, I ended having 2 questions:

  1. How can I add a Helper path to the PhpRenderer?
  2. Searching though the code, I found a class called ViewHelperManagerFactory, which has the default helper paths. How can I use it to change the HelperManager by a new one created by this factory with all the paths setted? (note I dont have a ServiceManager).

Upvotes: 0

Views: 672

Answers (1)

Elvan
Elvan

Reputation: 621

You can add helper by calling setFactory() from HelperPluginManager.

$renderer = new \Zend\View\Renderer\PhpRenderer();
$renderer->getHelperPluginManager()->setFactory('specialPurpose', function () {
    return new SpecialPurpose();
  });

echo $renderer->specialPurpose();
echo $renderer->specialPurpose();
echo $renderer->specialPurpose();

class SpecialPurpose extends \Zend\View\Helper\AbstractHelper {

  protected $count = 0;

  public function __invoke() {
    $this->count++;
    $output = sprintf("Called %d time(s).", $this->count);
    return htmlspecialchars($output, ENT_QUOTES, 'UTF-8');
  }

}

Upvotes: 3

Related Questions