简体   繁体   中英

Writing Custom Helpers in Zend Expressive 2

I'm writing a custom Helpers and everything works fine. but I have no idea how to pass parameters ...

on config/autoload/templates.global.php

return [
    'dependencies' => [
        'factories' => [
            TemplateRendererInterface::class => ZendViewRendererFactory::class,
            HelperPluginManager::class => HelperPluginManagerFactory::class,
        ],
    ],

    'templates' => [
        'layout' => 'layout::default',
    ],

    'view_helpers' => [
        'invokables' => [
            'HelperDatetimeFormat' => HelperDatetimeFormat::class,
        ],
        // zend-servicemanager-style configuration for adding view helpers:
        // - 'aliases'
        // - 'invokables'
        // - 'factories'
        // - 'abstract_factories'
        // - etc.
    ],
];

and I have this other:

namespace App\Helper;

use Locale;
use DateTime;
use IntlDateFormatter;

class HelperDatetimeFormat
{
    private $opts;
    private $lang;

    public function __invoke(array $opts)
    {
        $this->opts = $opts;
        return $this->datetimeFormat();
    }

    public function datetimeFormat()
    {
...

If I use a constructor I get an error ... is it advisable to pass parameters to build my class using __invoke ()?

Thanks!

__invoke is the function that will be called whenever you use your helper in a view. I should only take whatever you need from the view as parameter.

What is supposed to be in the array $opts ? If you need to inject something into this object (your last question), then you definitely need to use the constructor. The error you are receiving comes from the fact your View Helper is declared as Invokable (class with no parameter in the constructor), so the service manager is trying to create it without adding parameters. What you should do is move it to a factory.

'view_helpers' => [
    'aliases' => [
        'HelperDatetimeFormat' => HelperDatetimeFormat::class,
    ],
    'factories' => [
        HelperDatetimeFormat::class => HelperDatetimeFormatFactory::class,
    ],
],

And the factory is something similar to:

final class HelperDatetimeFormatFactory
{
    public function __invoke(ContainerInterface $container) : HelperDatetimeFormat
    {
        $params = $container->get('config')['my_params'];
        return new HelperDatetimeFormat($params);
    }
}

Beware, this code is more for ZF3. If I reckon the argument passed to __invoke in the factory is in fact HelperPluginManager , a ServiceLocatorInterface dedicated to view helpers, therefore, looking for a service declared in the main ServiceManager ( service_manager in the configuration array) will require an extra call on the HelperPluginManager :

public function __invoke(HelperPluginManager $viewHelperContainer) : HelperDatetimeFormat
{
    $params = $viewHelperContainer->getServiceLocator()->get('config')['my_params'];
    return new HelperDatetimeFormat($params);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM