Skip to content
This repository has been archived by the owner on Jan 29, 2020. It is now read-only.

How to setup form view helpers #217

Conversation

weierophinney
Copy link
Member

I have setup my Zend\Expressive application and really like the lightweight approach. But currently I am struggling to set up form view helpers to output a form in a view template. One issue I tracked down is due to the fact, that Zend\Form is not fully refactored to use the latest Zend\ServiceManager changes. But I am not sure if that is all to fix this.

I found some posts. The first is about integrating with Aura.DI:
http://harikt.com/blog/2015/11/13/integrating-zend-form-in-zend-expressive-and-view/

And the second with Zend\ServiceManager (which I could not get to run due to the current incompatibilities mentioned above):
http://www.masterzendframework.com/zend-expressive-enable-form-view-helpers/

It would be nice if the usage of form view helpers could be supported to help beginners adapt Zend\Expressive. I don't care if this can be done with an additional package, or within the Cookbook. At least the problem should be addressed to help the beginners.

What needs to be done to use the good old form view helpers within my Zend\Expressive project?

@weierophinney
Copy link
Member

@RalfEggert — I've converted this issue into a pull request containing updated (cookbook) documentation on how to use the helper configuration files from components to configure the HelperPluginManager, as well as how to add and configure custom helpers.

The documentation is based on zendframework/zend-expressive-skeleton#34, which updates the base zend-view configuration to ensure the HelperPluginManager factory is registered by default, and also to add the view_helpers configuration key to the templates configuration.

@harikt
Copy link
Contributor

harikt commented Dec 9, 2015

If you have any doubts using with Aura.Di, I am happy to help.

Thank you.

@weierophinney
Copy link
Member

@harikt Is there a way to extend/decorate a service in Aura.Di? If so, can you provide a gist? I can then update this PR to incorporate that information.

@harikt
Copy link
Contributor

harikt commented Dec 9, 2015

Hm. that is a tough question that I don't know or I am not getting the question. May be we should ping @pmjones .

@weierophinney
Copy link
Member

I've also added a section detailing a simpler method: replacing the HelperPluginManager factory with an application-specific one.

@harikt
Copy link
Contributor

harikt commented Dec 9, 2015

One thing probably I can tell you,

If you have registered a service

$di->set('Zend\View\HelperPluginManager', $di->lazyNew('Zend\View\HelperPluginManager'));

// middleware instantiation

$di->params['FormHelpersMiddleware']['helpers'] = $di->lazyGet('Zend\View\HelperPluginManager');

In your middleware you can inject the Zend\View\HelperPluginManager service .

use Zend\Form\View\HelperConfig;

class FormHelpersMiddleware
{
    private $helpers;

    public function __construct(HelperPluginManager $helpers)
    {
        $this->helpers = $helpers;
    }

    public function __invoke($request, $response, callable $next)
    {
        $config = new HelperConfig();
        $config->configureServiceManager($this->helpers);
        return $next($request, $response);
    }
}

Is this something you were looking for ?

The simplest, most portable way is to just replace the factory with your own.
@weierophinney weierophinney force-pushed the hotfix/zend-view-helpers-cookbook branch from 345d559 to afedeec Compare December 9, 2015 14:20
@harikt
Copy link
Contributor

harikt commented Dec 9, 2015

I noticed your commit little late ie why the first reply. Seems you already found some other way.

@weierophinney
Copy link
Member

@harikt I actually demonstrate the middleware-based solution already, and added another solution today that demonstrates simply replacing the HelperPluginManager factory. I think we're probably good at this point.

@harikt
Copy link
Contributor

harikt commented Dec 9, 2015

yes, I copied the middleware code from your commit. The only thing I added was the aura/di config.

I think we're probably good at this point.

great! .

@weierophinney
Copy link
Member

The only thing I added was the aura/di config.

It's unneeded; the config/container.php we use for Aura.Di can use both the invokables and factories configuration used by the other containers, meaning no changes are required to make it work. 😉

@weierophinney weierophinney merged commit afedeec into zendframework:master Dec 9, 2015
weierophinney added a commit that referenced this pull request Dec 9, 2015
weierophinney added a commit that referenced this pull request Dec 9, 2015
weierophinney added a commit that referenced this pull request Dec 9, 2015
@weierophinney weierophinney deleted the hotfix/zend-view-helpers-cookbook branch December 9, 2015 14:48
@RalfEggert
Copy link
Contributor Author

@weierophinney I will test it while working on the Album tutorial code and give some feedback here when finished...

@RalfEggert
Copy link
Contributor Author

@weierophinney tested the first solution with "Replacing the HelperPluginManager factory" and it worked like a charm

👍 nice

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants