【问题标题】:cannot find service filter plugin Zend Framework找不到服务过滤器插件 Zend Framework
【发布时间】:2017-10-16 01:48:44
【问题描述】:

我的过滤器有以下配置。用于zend框架设置服务管理器。

$filters = [
  'factories' => ['Administration\Filter\StripSpaces'=>'Zend\ServiceManager\Factory\InvokableFactory']
  'aliases'   => ['StripSpaces'=>'Administration\Filter\StripSpaces']
];
return ['filters'=>$filters];

表格使用

$inputFilter->add([
    'name'     => 'objectclassname',
    'required' => true,
    'filters'  => [
            ['name' => 'StringTrim'],
            ['name' => 'StripTags'],
            ['name' => 'StripNewlines'],
            ['name' => 'StripSpaces'] // here is where my StripSpaces alias is used
    ],
    'validators' => [                       
            [
             'name'    => 'StringLength',
             'options' => [
                'min' => 5,
                'max' => 255
              ],
         ]
     ],
]);

这是加载使用此过滤器的表单时的错误:

A plugin by the name "StripSpaces" was not found in the plugin manager Zend\Filter\FilterPluginManager

【问题讨论】:

  • @AlainPomirol 为什么我不能将它添加到服务管理器并添加别名而不是完成所有这些工作?他们希望我添加一个自定义过滤器并且他们具有相同的配置
  • 您将在docs.zendframework.com/zend-filter/filter-chains/… 的文档中找到解释。 FilterPluginManager 附加到每个 FilterChain 实例。 FilterChain 中使用的每个过滤器都必须为过滤器插件管理器所知。

标签: filter configuration zend-framework2 zend-framework3 zend-filter


【解决方案1】:

据我所知,ZF3 中没有名为 StripSpaces 的过滤器。

【讨论】:

  • 我知道它是我写的一个过滤器,但我无法让它像我所做的那样工作
【解决方案2】:

如果您想在 InputFilter 中包含自定义过滤器/验证器,您应该从 InputFilterManager 中检索 InputFilter 类,如下所示:

$serviceManager->get('InputFilterManager')->get(MyInputFilter::class);

或使用任何别名代替 FQCN,这取决于您注册 inputFilters 的方式。

为什么我需要从经理那里获取我的输入过滤器?由于当你创建一个新对象时,如new MyInputFilter()InputFilter\Factory 会创建一个InputFilterManager 类的新实例。这不是包含您的配置的应用程序InputFilterManager,因此只有默认的 Zend 过滤器/验证器。如果您没有要使用的任何自定义过滤器或验证器,则可以使用此方法。

当您使用应用程序InputFilterManager 获取输入过滤器时,它通过提供应用程序InputFilterManager 来获取输入过滤器updates the factory,其中包含对应用程序ServiceManager 的引用。从您的应用程序ServiceManager 中,它会为您的过滤器和验证器以及 InputFilter Factory 的updates the chains 获取其他管理器。所以 InputFilter 知道您的自定义过滤器/验证器。

请注意,您应该更新设置输入过滤器的方式。不要在__construct() 中设置您的过滤器/验证器,因为链尚未更新,因此不包含您的自定义过滤器/验证器。将您的输入过滤器配置移动到从InputFilterManager 调用的public function init(),其中initializes 您的InputFilter 类。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-06-10
    • 1970-01-01
    • 2012-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多