在您的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
]
],