ZF2:我如何实现自定义过滤器? [英] ZF2: How do I implement a custom filter?

查看:145
本文介绍了ZF2:我如何实现自定义过滤器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在使用Zend Framework 2几个星期了,尽管网上的文档很不完整,我仍然设法建立了我的网站的初稿。

不幸的是,我试图实现一个定制版本的Zend \ Filter \ File \ Rename过滤器,在这里,我总结了我所做的:
$ b $ 1)在我的模块名为图书馆,创建文件



src\Library\Filter\File\Rename.php


 命名空间Library\Filter\File ; 

使用Traversable;
使用Zend\Filter;
使用Zend\Filter\Exception;
使用Zend\Stdlib\ArrayUtils;
$ b $ class重命名扩展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)
抛出新的Exception \RuntimeException(sprintf(File'%s'无法重命名,处理文件时发生错误),$ value));

return self :: uploadDir。$ newname;
}
}

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

module.config.php

 < / p> ; $ php 
return array(
'controllers'=> array(
// Invokables不支持依赖关系)$ b $'invokables'=> array(
'myFileRename'=>'Library \Filter \File \Rename',
),
));

虽然表格是:

  [...] 
public函数getInputFilterSpecification(){
返回数组(
'file'=> array(
'required'=> false,
'filters'=> array(
// Custom Renamer
array('name'=>'myFileRename') ,
),

);
}
}

?>

但是这不起作用,因为我总是收到一个异常:

 致命错误:未捕获的异常'Zend\ServiceManager\Exception\ServiceNotFoundException'带有消息'Zend\ServiceManager\ServiceManager :: get无法获取或者在/srv/default/vendor/ZF2/library/Zend/ServiceManager/ServiceManager.php:420 
堆栈跟踪:
#0 / srv / default / vendor / ZF2中创建一个myFileRename实例。 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.ph p(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

不幸的是,我发现整个互联网上没有任何文章能够帮助我,而且即使在过滤器作为工厂时,我仍然得到这个错误:/我该怎么办?

解决方案

你加载过滤器就好像它是一个控制器(因此,在你的模块配置控制器键)。过滤器有一个插件管理器,但是对于这个过滤器你不需要插件管理器。过滤器插件管理器中也没有链接MVC配置。

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

 使用Library \Filter\File\Rename作为RenameFilter; 
$ b $ public function getInputFilterSpecification()
{
return array($ b $'file'=> array(
'required'=> false,
'filters'=>数组(
new RenameFilter,
),

);
}


I've been working with Zend Framework 2 for a few weeks now, and, even though the documentation online is pretty incomplete, I managed to build up a first draft of my website.

Unfortunately, I got stuck while trying to implement a custom version of the Zend\Filter\File\Rename filter; here I sum up what I've done:

1) In my module called 'Library', created the file

src\Library\Filter\File\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;
    }
}

As you can see, is pretty simple. Here's the module config:

module.config.php

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

While the form is:

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

?>

But this is not working, as I always get an exception:

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

Unfortunately I found no article on the entire internet to help me with this, and I still get this error even when aliasing my filter as a factory :/ What do I have to do?

解决方案

You load the filter as if it is a controller (hence, the controller key in your module config). The filter has a plugin manager, but for this filter you do not need the plugin manager. There is also no link in the filter plugin manager with the MVC configuration.

The most simple way is to just instantiate the filter yourself. It should be something like this that would work:

use Library\Filter\File\Rename as RenameFilter;

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

这篇关于ZF2:我如何实现自定义过滤器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆