ZendMvcControllerPluginManager::get 无法获取或创建 init 的实例



我正在维护一个PHP Zend应用程序。 我正在尝试向其添加功能。

我正在尝试通过 phtml 文件调用控制器。 我认为我以错误的方式接近这一点,但我不确定正确的方法是什么。

我修改了三个文件并添加了另一个文件。 我已经将代码添加到文件控制器.php。

public function getModifiedBy($filename) {
    $groupFiles =$this->getServiceLocator()->get('qatoolsModel       GroupFilesTable');
    $modified = $groupFiles->fetch($filename);
    return $modified;
}

我还在job-wizard.phtml中添加了代码。

<?php
use qatoolsControllerFileController;
$fileControl = new FileController;
$fileControl->init();
$modified =$fileControl->getModifiedBy("addresscleaningservice.xlsx");
?>

新文件是"GroupFileTable.php",它扩展了AbstractModelTable并查询MySQL数据库。 我将以下行添加到module.config.php。

'qatoolsModelGroupFilesTable' => function($sm) {
    return defModelTable($sm, 'GroupFilesTable');
    },
'GroupFilesTableGateway' => function($sm) {
        return defModelTableGateway($sm, 'group_files', 'GroupFiles');
            },

代码在 job-wizard.phtml 中的 $fileControl->init(( 上失败。 我尝试删除该行,但随后代码在getServiceLocator调用中失败。

#0 /mnt/c/git-repos/qatools/vendor/zendframework/zendframework/library     /Zend/ServiceManager/AbstractPluginManager.php(103): ZendServiceManagerServiceManager->get('init', true)
#1 /mnt/c/git-repos/qatools/vendor/zendframework/zendframework/library/Zend/Mvc/Controller/PluginManager.php(82): ZendServiceManagerAbstractPluginManager->get('init', NULL, true)

你没有指定你使用的是哪个版本的Zend Framework,反正不管是什么版本,你都不应该在视图脚本中创建控制器。

在 ZF 中,如果需要调用视图功能,更好的方法是创建一个视图助手,ZF1 和 ZF2/3 之间的逻辑是一样的,只是实现发生了变化,我就留给大家谷歌搜索看看示例,你最终的视图脚本代码应该是这样的

<?php echo $this->getModifiedBy("addresscleaningservice.xlsx"); ?>

视图助手将包含逻辑。如果在某个时候您需要在控制器或任何其他位置重用此逻辑,我的建议是使用该逻辑创建一个组件,然后创建一个返回此组件实例的服务,并最终创建一个操作帮助程序(任何控制器中的 $this->myComponent(和一个视图帮助程序(任何视图中的 $this->myComponent(并注入相同的组件实例。

最新更新