duanlujiaji10335 2014-06-12 15:04
浏览 12
已采纳

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

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...

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();. 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'). I think I am missing something fundamental here.

  • 写回答

1条回答 默认 最新

  • doude4924 2014-06-12 15:18
    关注

    The init() method invoked automatically by InputFilterManager just after the filter object created. 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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#java#的问题,请各位专家解答!
  • ¥15 急matlab编程仿真二阶震荡系统
  • ¥20 TEC-9的数据通路实验
  • ¥15 ue5 .3之前好好的现在只要是激活关卡就会崩溃
  • ¥50 MATLAB实现圆柱体容器内球形颗粒堆积
  • ¥15 python如何将动态的多个子列表,拼接后进行集合的交集
  • ¥20 vitis-ai量化基于pytorch框架下的yolov5模型
  • ¥15 如何实现H5在QQ平台上的二次分享卡片效果?
  • ¥30 求解达问题(有红包)
  • ¥15 请解包一个pak文件