ZF2:如何实现自定义过滤器?(ZF2: How do I implement a custom f

2019-09-23 20:19发布

我一直在与Zend框架2几周现在,而且,即使在线文档是相当不完整的,我设法建立我的网站的第一稿。

不幸的是,在落实了Zend \过滤器\文件的定制版本\重命名过滤器我被困; 在这里我总结了我所做的事情:

1)在我的模块被称为“库”,创建的文件

SRC \图书馆\过滤器\文件\ Rename.php

 namespace Library\Filter\File; use Traversable; use Zend\Filter; use Zend\Filter\Exception; use Zend\Stdlib\ArrayUtils; class Rename extends Filter\AbstractFilter { static $uploadDir = '/srv/default/htdocs/public/uploads/'; public function filter($value) { do { $newname = md5(uniqid(time())); } while(file_exists(self::uploadDir . $newname); if (rename($value, self::uploadDir . $newname) !== true) throw new Exception\RuntimeException(sprintf("File '%s' could not be renamed. An error occurred while processing the file.", $value)); return self::uploadDir . $newname; } } 

正如你所看到的,是非常简单的。 这里的模块配置:

module.config.php

<?php
    return array(
        'controllers' => array(
            // Invokables don't support dependencies
            'invokables'    => array(
                'myFileRename'    => 'Library\Filter\File\Rename',
            ),
    ));

虽然形式是:

[...]
    public function getInputFilterSpecification() {
        return array(
            'file' => array(
                'required'  => false,
                'filters'   => array(
                    // Custom Renamer
                    array('name' => 'myFileRename'),
                ),
            )
        );
    }
 }

?>

但是,这是行不通的,因为我总是得到一个异常:

Fatal error:  Uncaught exception 'Zend\ServiceManager\Exception\ServiceNotFoundException' with message 'Zend\ServiceManager\ServiceManager::get was unable to fetch or create an instance for myFileRename' in /srv/default/vendor/ZF2/library/Zend/ServiceManager/ServiceManager.php:420
Stack trace:
#0 /srv/default/vendor/ZF2/library/Zend/ServiceManager/AbstractPluginManager.php(108): Zend\ServiceManager\ServiceManager->get('myfilerename', true)
#1 /srv/default/vendor/ZF2/library/Zend/Filter/FilterChain.php(179): Zend\ServiceManager\AbstractPluginManager->get('myfilerename', NULL)
#2 /srv/default/vendor/ZF2/library/Zend/InputFilter/Factory.php(263): Zend\Filter\FilterChain->attachByName('myfilerename', Array)
#3 /srv/default/vendor/ZF2/library/Zend/InputFilter/Factory.php(171): Zend\InputFilter\Factory->populateFilters(Object(Zend\Filter\FilterChain), Array)
#4 /srv/default/vendor/ZF2/library/Zend/InputFilter/Factory.php(237): Zend\InputFilter\Factory->createInput(Array)
#5 /srv/default/vendor/ZF2/library/Zend/Form/Form.php(672) in /srv/default/vendor/ZF2/library/Zend/ServiceManager/ServiceManager.php on line 420

不幸的是,我发现整个互联网上没有文章帮助我,和我混叠滤波器作为一个工厂时,我仍然得到这个错误甚至:/我有什么做的?

Answer 1:

就好像它是一个控制器(因此,您加载过滤controller在你的模块配置的关键)。 该过滤器有一个插件管理器,但该过滤器,你不需要插件管理器。 也有在过滤器插件管理器与MVC框架的配置没有任何联系。

最简单的方法就是刚刚自己实例化过滤器。 它应该是这样的,将工作:

use Library\Filter\File\Rename as RenameFilter;

public function getInputFilterSpecification()
{
    return array(
        'file' => array(
            'required'  => false,
            'filters'   => array(
                new RenameFilter,
            ),
        )
    );
}


Answer 2:

只需使用:

'name' => 'file',
'required' => false, 
'filters' => array(
     array('name' => 'Library\Filter\File\Rename'),
),

有同样的问题......这是我的解决方案...



文章来源: ZF2: How do I implement a custom filter?