How to use InputFilterManager to construct custom InputFilters in Zf2

后端 未结 1 869
攒了一身酷
攒了一身酷 2021-02-06 02:19

ZF2 documentation says following on defult services documentation;

InputFilterManager, mapping to Zend\\Mvc\\Service\\InputFilterManagerFactory. This

相关标签:
1条回答
  • 2021-02-06 02:37

    Ok, after spending 3 bloody hours (thanks to incredible(!) documentation) I figured it out. I'm writing my solution as an answer, hopefully it will help others who want to write their custom inputfilters.

    1. You should register your custom inputfilter in module.config.php by input_filters top key, not filter, filters, filter_manger, filtermanager etc..
    2. Extend default Zend\InputFilter\InputFilter when writing your own GlassFilter.
    3. Write your filters inside the init() method of GlassFilter, not in the __constructor(). It will be called automatically after construction.
    4. Then get it anywhere via inputfiltermanager, not servicemanager directly.

    Config example:

    'input_filters' => array(
        'invokables' => array(
            'glassfilter' => '\Application\Filter\GlassFilter',
         ),
    ),
    

    Usage example:

    $glassfilter = $serviceLocator->get('InputFilterManager')->get('glassfilter');
    
    0 讨论(0)
提交回复
热议问题