1. 云栖社区>
  2. PHP教程>
  3. 正文

Integrating Zend Form in Zend Expressive and View

作者:用户 来源:互联网 时间:2017-12-01 12:47:08

Integrating Zend Form in Zend Expressive and View - 摘要: 本文讲的是Integrating Zend Form in Zend Expressive and View, Example is based using Aura.Di. But the functionality will be same for any containers. First register the service

Example is based using Aura.Di. But the functionality will be same for any containers. First register the service Zend/View/HelperPluginManager, so that we can access the same object.

To register the form helpers, create the object of Zend/Form/View/HelperConfigand pass the Zend/View/HelperPluginManagerservice.

Example code with Aura.Diversion 3 configuration.

<?phpuse Aura/Di/ContainerConfig;use Aura/Di/Container;use Zend/Form/View/HelperConfig;class ViewHelper extends ContainerConfig{ public function define(Container $di) { $di->set('Zend/View/HelperPluginManager', $di->lazyNew('Zend/View/HelperPluginManager')); } public function modify(Container $di) { $serviceManager = $di->get('Zend/View/HelperPluginManager'); $helper = new HelperConfig(); $helper->configureServiceManager($servicemanager); }} Creating your own zend-view helper Create your helper class <?phpnamespace App/View/Helper;use Zend/View/Helper/AbstractHelper;class HasError extends AbstractHelper{ // add as many parameters you want to pass from the view public function __invoke() { // some code }} Registering your helper class.

First get the Zend/View/HelperPluginManagerservice.

2.a ) Registering as a factory

$serviceManager->setFactory('hasError', function () { return new /App/View/Helper/HasError();});

2.b ) As an invokable

$serviceManager->setInvokableClass('hasError', 'App/View/Helper/HasError');

Now you can access inside zend-view as $this->hasError(). If your view helper need dependencies don’t use the setInvokableClassmethod. Use factory and get the object from the container.

$serviceManager->setFactory('hasError', function () use ($di) { return $di->get('App/View/Helper/HasError');});

I wished if $serviceManagercan understand the aura’s lazyNewfunctionality so that we don’t need to register it as a service.

Eg : Below will not work.

$serviceManager->setFactory('hasError', $di->lazyNew('App/View/Helper/HasError'));

This is what I love to see it working for this a closure, but with namespaced.

以上是云栖社区小编为您精心准备的的内容,在云栖社区的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索,以便于您获取更多的相关知识。