Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ZF2: How do I implement a custom filter?

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?

like image 877
h3st14 Avatar asked Nov 03 '22 14:11

h3st14


1 Answers

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,
            ),
        )
    );
}
like image 166
Jurian Sluiman Avatar answered Nov 08 '22 09:11

Jurian Sluiman