PluginManager::get 无法获取或创建 FileQueryPlugin 的实例

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

我正在使用 Zend Framework 2.3.9。我正在尝试使用依赖注入从视图助手调用插件 'FileQueryPlugin'。

我尝试在 module.config.php 中将 'plugins' 更改为 'controller_plugins',但我得到了相同错误消息的变体。

这里是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'
    ),
),

这里是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) {

    }
}
?>

我看到以下错误消息。

Zend\ServiceManager\Exception\ServiceNotFoundException

文件:

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

留言:

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

堆栈跟踪:

#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}

您的命名不一致。

您的工厂函数正在调用以下内容:

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

而你的 class 名字是 FileQueryPlugin

您应该重命名 class 或调用 new \qatools\View\Helper\FileQueryPlugin(...);

更新

您的 FileQueryPlugin.php 文件中有两个名称空间声明,您需要删除下面的那个,上面的应该与您在工厂中调用的那个相对应 (\qatools\View\Helper\FileQueryPlugin:

namespace qatools\Plugins; // <--- update this so it corresponds

namespace Zend\View;  // <--- remove this one

您还应确保文件所在的文件夹对应于路径 \qatools\View\Helper\FileQuery,因此文件夹树应如下所示:

- qatools
   - View 
     - Helper
       - FileQueryPlugin.php