从 View Helper 调用 Zend 插件 class

Calling Zend Plugin from View Helper class

我想从视图助手中调用 zend 插件 class。我正在使用 zend 2.3。我有一个 job-wizard.phtml 文件,我需要添加代码来识别文件的所有者。我有一个 GroupFilesTable.php 文件,它扩展了 AbstractModelTable 并且能够获取文件的所有者。

我创建了一个名为 'FileQuery' 的视图助手 class。由于我需要调用 getServiceLocator 来访问 GroupFilesTable,因此我创建了一个 FileQuery 调用的 FileQueryPlugin。

但是我 运行 在调用 FileQueryPlugin 时出错。

我尝试将 FileQuery 视图助手从 AbstractHelper 扩展到 AbstractPluginManager,但这样做时出现错误。

来自工作-wizard.phtml

<?php $modifiedBy  =$this->FileQuery()->getModifiedBy('addresscleaningservice.xlsx');
?>\

来自filequery.php

class FileQuery extends AbstractHelper {
    public function getModifiedBy($filename) {
        $fileQuery = $this->FileQueryPlugin();
        $owner = $fileQuery->getModifiedBy($filename);
        return $filename;
    }
}

来自filequeryplugin.php

class FileQueryPlugin extends AbstractPluginManager {

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

        return $modified;
    }
}

摘自module.config.php

'view_helpers' => array(
    'invokables'=> array(
        'MenuBuildLink' => 'qatools\View\Helper\MenuBuildLink',
        'FileQuery' => 'qatools\View\Helper\FileQuery'
    ),
),
'plugins' => array(
    'invokables' => array(
        'FileQueryPlugin' => 'qatools\Plugins\FileQueryPlugin'
    ),
),

我看到这条消息表明我可能没有正确设置 module.config.php。

[26-Jul-2019 09:53:19 America/Chicago] PHP Fatal error:  Uncaught Error: Call to undefined method qatools\View\Helper\FileQuery::FileQueryPlugin() in /mnt/c/git-repos/qatools/module/qatools/src/qatools/View/Helper/FileQuery.php:9
Stack trace:
#0 /mnt/c/git-repos/qatools/module/qatools/view/partials/job-wizard.phtml(4735): qatools\View\Helper\FileQuery->getModifiedBy('addresscleaning...')
#1 /mnt/c/git-repos/qatools/vendor/zendframework/zendframework/library/Zend/View/Renderer/PhpRenderer.php(506): include('/mnt/c/git-repo...')
#2 /mnt/c/git-repos/qatools/vendor/zendframework/zendframework/library/Zend/View/Helper/Partial.php(61): Zend\View\Renderer\PhpRenderer->render(NULL, NULL)
#3 [internal function]: Zend\View\Helper\Partial->__invoke('partials/job-wi...')
#4 /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)
#5 /mnt/c/git-repos/qatools/module/qatools/view/qatools/jobs/index.phtml(1064): Zend\View\Renderer\PhpRenderer->__call('partial', Array)
 in /mnt/c/git-repos/qatools/module/qatools/src/qatools/View/Helper/FileQuery.php on line 9

您应该更改 FileQuery 的 viewhelper 设置。使用依赖注入将控制器插件注入到您的 viewhelper 中。这可以通过创建工厂 class 或为您的 FileQuery viewhelper 调用来实现。

参见:https://docs.zendframework.com/zend-servicemanager/configuring-the-service-manager/#factories

在你的情况下,这将归结为 module.config.php:

    'view_helpers' => array(
        'invokables'=> array(
            'MenuBuildLink' => 'qatools\View\Helper\MenuBuildLink',
        ),
        'factories' => array(
            'FileQuery' => function ($container, $requestedName) {
                # Using an older version of Zend - the container is the ViewHelperManager and not the main ServiceManager so getServiceLocator() should be called.
                # return new \qatools\View\Helper\FileQuery($container->getServiceLocator()->get(\Zend\Mvc\Controller\PluginManager::class)->get('FileQueryPlugin')));
                return new $requestedName($container->get(\Zend\Mvc\Controller\PluginManager::class)->get('FileQueryPlugin'));
            },
        ),
    ),
    'plugins' => array(
        'invokables' => array(
            'FileQueryPlugin' => 'qatools\Plugins\FileQueryPlugin'
        ),
    ),

或者如文档所述,您可以创建一个工厂 class,它会创建一个新的 viewhelper。

并更新您的 viewhelper,使其在构造函​​数中接受 FileQueryPlugin。

class FileQuery extends AbstractHelper {

    protected $fileQueryPlugin;

    public function __construct(FileQueryPlugin $fileQueryPlugin)
    {
        $this->fileQueryPlugin = $fileQueryPlugin;
    }

    public function getModifiedBy($filename) 
    {
        return $this->fileQueryPlugin->getModifiedBy($filename);
    }
}