問題描述
將外部腳本集成到 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 命名約定以找到它們.
這篇關于將外部腳本與 Zend Framework 集成的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!