将外部脚本与 Zend Framework 集成

2021-12-29 00:00:00 php zend-framework

将外部脚本集成到 Zend 框架的最佳方法是什么?让我解释一下,因为我可能问错了.我有一个下载和解析 XML 文件的脚本.该脚本作为日常 cron 作业运行,需要将其数据转储到数据库中.

What is the best way to integrate an external script into the Zend Framework? Let me explain because I may be asking this the wrong way. I have a script that downloads and parses an XML file. This script, which runs as a daily cron job, needs to dump its data into the database.

我正在为使用此脚本的站点使用 Zend Framework,在我看来,最好使用我的 Zend_Db_Abstract 子类模型来添加和更新数据库.怎么做呢?我的脚本是否位于 Zend 组件旁边的库中(即 library/Mine/Xmlparse.php),从而可以访问各种 ZF 组件?我是否只需要在文件本身中包含正确的模型文件和 Zend DB 组件?处理这种集成的最佳方法是什么?

I am using Zend Framework for the site which uses this script and it seems to me that it would be best to use my subclassed model of Zend_Db_Abstract to do the adding and updating of the database. How does one go about doing this? Does my script go in the library next to the Zend Components (i.e. library/Mine/Xmlparse.php) and thus have access to the various ZF components? Do I simply need to include the correct model files and the Zend DB component in the file itself? What is the best way to handle this sort of integration?

推荐答案

在您的库目录中,您应该在 Zend 库文件夹旁边有您自己的库.无论你怎么称呼它(Mylib、Project 等),你都应该将它包含到 Zend Autoloader 中,具体操作如下:

In your library directory you should have your own library next to the Zend library folder. Whatever you call it (Mylib, Project, ...) you should include it into the Zend Autoloader and that's done as follows:

require_once 'Zend/Loader/Autoloader.php';
$loader = Zend_Loader_Autoloader::getInstance();
$loader->registerNamespace('Project_');
$loader->setFallbackAutoloader(true);
if ($configSection == 'development')
{
    $loader->suppressNotFoundWarnings(false);
}

为了让您的库与 ZF 和 Autoloader 很好地集成,您应该坚持 ZF 命名约定.这意味着两件事:

In order for you library to integrate nicely with ZF and the Autoloader you should stick to the ZF naming conventions. This means two things:

  • 如果您扩展现有的 ZF 类,请复制 ZF 文件夹结构,以便您的文件具有相同的路径和名称,但库名称除外.例如./library/Zend/Db/Abstract.php =>/library/Project/Db/Abstract.php.
  • 如果您编写自己的类,仍要坚持自动加载器的 ZF 命名约定以找到它们.

相关文章