Search code examples
phpzend-framework2

PluginManager::get was unable to fetch or create an instance for FileQueryPlugin


I'm using Zend Framework 2.3.9. I'm trying to call a plugin 'FileQueryPlugin' from a view helper using dependency injection.

I tried changing 'plugins' to 'controller_plugins' in module.config.php, but I got a variation of the same error message.

Here is module.config.php

'view_helpers' => array(
    'invokables'=> array(
        'MenuBuildLink' => 'qatools\View\Helper\MenuBuildLink',
    ),
    'factories' => array(
        'FileQuery' => function ($container, $requestedName) {
            return new \qatools\View\Helper\FileQuery($container->getServiceLocator()->get(\Zend\Mvc\Controller\PluginManager::class)->get('FileQueryPlugin'));
        },
    ),
),
'plugins' => array(
    'invokables'=> array(
        'FileQueryPlugin' => 'qatools\Plugins\FileQueryPlugin'
    ),
),

Here is FileQueryPlugin.php

<?php

namespace qatools\Plugins;

namespace Zend\View;

use Zend\I18n\Translator\TranslatorAwareInterface;
use Zend\ServiceManager\AbstractPluginManager;
use Zend\ServiceManager\ConfigInterface;

class FileQueryPlugin extends AbstractPluginManager {

    public function fileQuery($filename) {
        $fileQuery = $this->getServiceLocator->get('qatools\Model\GroupFilesTable');
        $modified = $fileQuery->getModifiedBy($filename);

        return $modified;
    }

    public function validatePlugin($plugin) {

    }
}
?>

I'm seeing the following error message.

Zend\ServiceManager\Exception\ServiceNotFoundException

File:

/mnt/c/git-repos/qatools/vendor/zendframework/zendframework/library/Zend/ServiceManager/ServiceManager.php:550

Message:

Zend\Mvc\Controller\PluginManager::get was unable to fetch or create an instance for FileQueryPlugin

Stack trace:

#0 /mnt/c/git-repos/qatools/vendor/zendframework/zendframework/library/Zend/ServiceManager/AbstractPluginManager.php(103): Zend\ServiceManager\ServiceManager->get('FileQueryPlugin', true)
#1 /mnt/c/git-repos/qatools/vendor/zendframework/zendframework/library/Zend/Mvc/Controller/PluginManager.php(82): Zend\ServiceManager\AbstractPluginManager->get('FileQueryPlugin', Array, true)
#2 /mnt/c/git-repos/qatools/module/qatools/config/module.config.php(405): Zend\Mvc\Controller\PluginManager->get('FileQueryPlugin')
#3 [internal function]: qatools\Module->{closure}(Object(Zend\View\HelperPluginManager), 'filequery', 'FileQuery')
#4 /mnt/c/git-repos/qatools/vendor/zendframework/zendframework/library/Zend/ServiceManager/ServiceManager.php(923): call_user_func(Object(Closure), Object(Zend\View\HelperPluginManager), 'filequery', 'FileQuery')
#5 /mnt/c/git-repos/qatools/vendor/zendframework/zendframework/library/Zend/ServiceManager/AbstractPluginManager.php(259): Zend\ServiceManager\ServiceManager->createServiceViaCallback(Object(Closure), 'filequery', 'FileQuery')
#6 /mnt/c/git-repos/qatools/vendor/zendframework/zendframework/library/Zend/ServiceManager/AbstractPluginManager.php(217): Zend\ServiceManager\AbstractPluginManager->createServiceViaCallback(Object(Closure), 'filequery', 'FileQuery')
#7 /mnt/c/git-repos/qatools/vendor/zendframework/zendframework/library/Zend/ServiceManager/ServiceManager.php(633): Zend\ServiceManager\AbstractPluginManager->createFromFactory('filequery', 'FileQuery')
#8 /mnt/c/git-repos/qatools/vendor/zendframework/zendframework/library/Zend/ServiceManager/ServiceManager.php(593): Zend\ServiceManager\ServiceManager->doCreate('FileQuery', 'filequery')
#9 /mnt/c/git-repos/qatools/vendor/zendframework/zendframework/library/Zend/ServiceManager/ServiceManager.php(525): Zend\ServiceManager\ServiceManager->create(Array)
#10 /mnt/c/git-repos/qatools/vendor/zendframework/zendframework/library/Zend/ServiceManager/AbstractPluginManager.php(103): Zend\ServiceManager\ServiceManager->get('FileQuery', true)
#11 /mnt/c/git-repos/qatools/vendor/zendframework/zendframework/library/Zend/View/Renderer/PhpRenderer.php(377): Zend\ServiceManager\AbstractPluginManager->get('FileQuery', NULL)
#12 /mnt/c/git-repos/qatools/vendor/zendframework/zendframework/library/Zend/View/Renderer/PhpRenderer.php(396): Zend\View\Renderer\PhpRenderer->plugin('FileQuery')
#13 /mnt/c/git-repos/qatools/module/qatools/view/partials/job-wizard.phtml(4740): Zend\View\Renderer\PhpRenderer->__call('FileQuery', Array)
#14 /mnt/c/git-repos/qatools/vendor/zendframework/zendframework/library/Zend/View/Renderer/PhpRenderer.php(506): include('/mnt/c/git-repo...')
#15 /mnt/c/git-repos/qatools/vendor/zendframework/zendframework/library/Zend/View/Helper/Partial.php(61): Zend\View\Renderer\PhpRenderer->render(NULL, NULL)
#16 [internal function]: Zend\View\Helper\Partial->__invoke('partials/job-wi...')
#17 /mnt/c/git-repos/qatools/vendor/zendframework/zendframework/library/Zend/View/Renderer/PhpRenderer.php(399): call_user_func_array(Object(Zend\View\Helper\Partial), Array)
#18 /mnt/c/git-repos/qatools/module/qatools/view/qatools/jobs/index.phtml(1064): Zend\View\Renderer\PhpRenderer->__call('partial', Array)
#19 /mnt/c/git-repos/qatools/vendor/zendframework/zendframework/library/Zend/View/Renderer/PhpRenderer.php(506): include('/mnt/c/git-repo...')
#20 /mnt/c/git-repos/qatools/vendor/zendframework/zendframework/library/Zend/View/View.php(205): Zend\View\Renderer\PhpRenderer->render(NULL)
#21 /mnt/c/git-repos/qatools/vendor/zendframework/zendframework/library/Zend/View/View.php(233): Zend\View\View->render(Object(Zend\View\Model\ViewModel))
#22 /mnt/c/git-repos/qatools/vendor/zendframework/zendframework/library/Zend/View/View.php(198): Zend\View\View->renderChildren(Object(Zend\View\Model\ViewModel))
#23 /mnt/c/git-repos/qatools/vendor/zendframework/zendframework/library/Zend/Mvc/View/Http/DefaultRenderingStrategy.php(103): Zend\View\View->render(Object(Zend\View\Model\ViewModel))
#24 [internal function]: Zend\Mvc\View\Http\DefaultRenderingStrategy->render(Object(Zend\Mvc\MvcEvent))
#25 /mnt/c/git-repos/qatools/vendor/zendframework/zendframework/library/Zend/EventManager/EventManager.php(468): call_user_func(Array, Object(Zend\Mvc\MvcEvent))
#26 /mnt/c/git-repos/qatools/vendor/zendframework/zendframework/library/Zend/EventManager/EventManager.php(207): Zend\EventManager\EventManager->triggerListeners('render', Object(Zend\Mvc\MvcEvent), Array)
#27 /mnt/c/git-repos/qatools/vendor/zendframework/zendframework/library/Zend/Mvc/Application.php(352): Zend\EventManager\EventManager->trigger('render', Object(Zend\Mvc\MvcEvent))
#28 /mnt/c/git-repos/qatools/vendor/zendframework/zendframework/library/Zend/Mvc/Application.php(327): Zend\Mvc\Application->completeRequest(Object(Zend\Mvc\MvcEvent))
#29 /mnt/c/git-repos/qatools/public/index.php(22): Zend\Mvc\Application->run()
#30 {main}

Solution

  • You have inconsistencies in your naming.

    Your factory function is calling following:

    return new \qatools\View\Helper\FileQuery(...);
    

    While your classname is FileQueryPlugin.

    You should rename the class or call new \qatools\View\Helper\FileQueryPlugin(...);

    UPDATE

    You have two namespace declarations in your FileQueryPlugin.php file you need to remove the lower one, and the upper one should correspond with the one you call in your factory (\qatools\View\Helper\FileQueryPlugin:

    namespace qatools\Plugins; // <--- update this so it corresponds
    
    namespace Zend\View;  // <--- remove this one
    

    You should also make sure the folder where the file resides corresponds to the path \qatools\View\Helper\FileQuery, so the folder tree should look like this:

    - qatools
       - View 
         - Helper
           - FileQueryPlugin.php