简体   繁体   English

zf2尝试将输入过滤器注入服务

[英]zf2 Trying to inject input filter into service

I'm attempting to create a custom filter and inject it into a service via factory. 我正在尝试创建自定义过滤器,并将其通过工厂注入到服务中。

use Zend\InputFilter\InputFilter; 
class WSRequestFilter extends InputFilter{

    protected $inputFilter;

    public function init(){
        $this->add( array(
            'name' => 'apiVersion',
            'required' => true,
            'filters' => [
                array('name' => 'Real'),
...

In Module.php... 在Module.php中...

public function getServiceConfig(){
    return array(
        ...
        'factories' => array(
            'Puma\Service\WebServiceLayer' => function($sm) {
                $wsRequestFilter = new Filter\WSRequestFilter();
                $wsRequestFilter->init();
                $wsl = new Service\WebServiceLayer($wsRequestFilter);
                return $wsl;
            },
        ),
    );
}

But I get service not found exception when executing $wsRequestFilter->init(); 但是执行$wsRequestFilter->init();时,我发现服务未找到异常$wsRequestFilter->init(); . I have also tried to initialize the filter using the InputFilterManager similar to here but I got a service not found trying to access the manager via $serviceManager->get('InputFilterManager') . 我也尝试过使用InputFilterManager来初始化过滤器,类似于这里,但是我找不到试图通过$serviceManager->get('InputFilterManager')访问管理器的$serviceManager->get('InputFilterManager') I think I am missing something fundamental here. 我想我这里缺少基本的东西。

The init() method invoked automatically by InputFilterManager just after the filter object created. 在创建过滤器对象之后,InputFilterManager会自动调用init()方法。 You don't need to invoke manually. 您无需手动调用。

Add this to your module configuration: 将此添加到您的模块配置:

'input_filters' => array(
    'invokables' => array(
        'ws-request-filter' => '\YourModule\Filter\WSRequestFilter',
     ),
),

And change your service factory like below: 并如下更改您的服务工厂:

public function getServiceConfig(){
    return array(
        ...
        'factories' => array(
            'Puma\Service\WebServiceLayer' => function($sm) {
                $filter = $sm->get('InputfilterManager')->get('ws-request-filter')
                $wsl = new \YourModule\Service\WebServiceLayer($filter);
                return $wsl;
            },
        ),
    );
}

It should work. 它应该工作。

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

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