简体   繁体   English

在Zend Expressive 2中编写自定义帮助程序

[英]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 在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 ()? 如果我使用构造函数,则会收到错误消息……建议使用__invoke()传递参数来构建类吗?

Thanks! 谢谢!

__invoke is the function that will be called whenever you use your helper in a view. __invoke是您在视图中使用助手时将调用的函数。 I should only take whatever you need from the view as parameter. 我应该只将视图中需要的任何内容用作参数。

What is supposed to be in the array $opts ? $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. 您收到的错误是由于您的View Helper被声明为Invokable (构造函数中没有参数的类)而引起的,因此服务管理器试图在不添加参数的情况下创建它。 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. 当心,此代码对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 : 如果我认为在工厂中传递给__invoke的参数实际上是HelperPluginManager ,它是专门用于查看助手的ServiceLocatorInterface ,因此,要在主ServiceManager声明的ServiceManager (配置数组中的service_manager )将需要对HelperPluginManager进行额外的调用:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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