0

我正在使用 Zend Framework 2,并且我创建了一个新的输入过滤器,它将在我的整个应用程序中使用。我知道我需要将新过滤器添加到 FilterPluginManager 的列表中,以便在创建输入时调用它。这是我认为我需要使用的代码:

\Zend\Filter\FilterPluginManager::setInvokableClass('myFilter', 'Namespace\Filters\MyFilter');

但是,我找不到这应该去哪里。这应该在引导程序,配置文件之一,还是其他地方?

4

2 回答 2

0

这就是你想要的答案

https://packages.zendframework.com/docs/latest/manual/en/modules/zend.service-manager.quick-start.html

class Module
{
public function getServiceConfig()
{
    return array(
        'invokables' => array(
           'UserInputFiler' => 'SomeModule\InputFilter\User',
        ),

    );
}
}
于 2013-07-20T03:40:20.640 回答
0

在您的Module中,实现InputFilterProviderInterface并为Factory( InputFilterPluginManager)提供一个AbstractPluginManager

namespace YourModule; 

use Zend\ModuleManager\Feature\InputFilterProviderInterface;

class Module implements InputFilterProviderInterface
{
    public function getInputFilterConfig()
    {
        return [
            'factories'=>[
                'your_input_filter_name'=>\YourModule\Factories\InputFilterFactory::class
            ]
        ];
    }
}

现在实现工厂:

namespace YourModule\Factories;

use Zend\Filter\StringTrim;
use Zend\InputFilter\Input;
use Zend\InputFilter\InputFilter;
use Zend\ServiceManager\FactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
use Zend\Validator\StringLength;

class InputFilterFactory implements FactoryInterface
{
    public function createService(ServiceLocatorInterface $serviceLocator)
    {
        $filter = new InputFilter();
        $nameFilter = new Input("name");
        $nameFilter->getValidatorChain()->attach(new StringLength([
            'min'=>3,
            'max'=>16
        ]));
        $nameFilter->getFilterChain()->attach(new StringTrim());
        $filter->add($nameFilter);
        return $filter;
    }
}

现在在您Controller需要的 -factory 或其他工厂中,InputFilter您可以访问InputFilterPluginManager

$inputFilterManager = $controllerManager->getServiceLocator()->get('InputFilterManager');

return new YourController(
    $inputFilterManager->get('your_input_filter_name')
);

更新,也可以在module.config.php中添加工厂

    'input_filters'=>[
        'factories'=>[
            'your_input_filter_name'=>InputFilterFactory::class
        ]
    ],
于 2021-10-13T14:21:11.860 回答