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