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).
4

1 回答 1

3

您可以通过从 HelperPluginManager 调用 setFactory() 来添加助手。

$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');
  }

}
于 2012-10-06T03:01:08.250 回答