Integration of external scripts with Zend Framework

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

I am using the Zend Framework for a site that uses this script, and it seems to me that it is best to use my subclassical model Zend_Db_Abstract to add and update the database. How can I do that? My script goes into the library next to the Zend components (i.e. Library / Mine / Xmlparse.php) and therefore has access to various ZF components? Do you just need to include the correct model files and Zend DB components in the file itself? What is the best way to handle this integration?

+4
source share
6 answers

In your library directory, you should have your own library next to the Zend library library. Whatever you call it (Mylib, Project, ...), you should include it in the Zend autoloader and do the following:

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

In order for the library to integrate well with ZF and the autoloader, you must adhere to the ZF naming conventions. This means two things:

  • If you extend the existing ZF class, copy the structure of the ZF folder so that your file has the same path and name, except for the library name. For instance. /library/Zend/Db/Abstract.php => /library/Project/Db/Abstract.php.
  • If you are writing your own classes, still stick with the ZF naming conventions for the autoloader to find them.
+2
source

Yes, you should put your own classes, which may inherit Zend Framework classes or add additional classes to your own folder next to the Zend Framework folder in the library.

When automatic loading of Zend_Loader is enabled, class names are automatically mapped to the created class, for example:

 My_Db_Abstract will map to My/Db/Abstract.php . 
+3
source

I just came across something that might be related to this issue. This article is from IBM developerWorks.

The author recommends simply creating a script folder in the ZF hierarchy and using it, as usual, in ZF (although he set the ini path and caused autoload). It is so simple? Is it just in the hierarchy of the framework and includes a path and autoloader, giving your script access to all the pluses?

+1
source

I am not 100% sure what you are trying to ask, but I will try to help. If at some point you add a link to "/ path / to / zend / framework" in your path to include php, you essentially included the Zend Framework. From there if you do this:

 require_once('Zend/Loader.php'); Zend_Loader::registerAutoload(); 

Then, at any point in your script, you can simply create new Zend Framework objects, and Zend_Loader will handle the rest.

One of the great things about the Zend Framework, however, is not forcing you to do things in a specific way. Therefore, sometimes there are several ways to do the same thing. So, if you think you need to use a script to use the Zend Framework just for the sake of this, this is not necessary. But if you think this can improve your script in some way, then go for it.

+1
source

Usually I put custom materials that I think can be used in projects in a user folder in the library. So I have an Ak33m library / folder that has scripts that may be out of scope.

+1
source

Like ZF noob himself, I think I understand some of what the OP is trying to figure out. So, I’ll just explain that I understand in the hope that this is useful either to the OP (or, rather, to the future reader, since the original question is so old and I believe that the OP is now a ZF guru).

I understand that ZF claims it pretty much β€œuses it as it sees fit," so you don’t have to buy the whole structure like Zend_Application, Zend_Bootstrap class, the whole MVC approach, etc.

In addition, I understand the conventions for class names and file locations, which make it easy to load startup. Example: class App_Model_User is located in the App/Model/User.php

I think it could be potentially confusing what is in the context of the script where you are not

  • made .htaccess magic that repels the whole request public/index.php
  • set APPLICATION_PATH and include the paths in public/index.php
  • created your Application or Bootstrap object bound to the configuration file

it may be a little unclear what is the best way to take advantage of most of the ZF kindness we get in this context and want in a different context.

I think my answer to the original question would be that the usual sequence of entry points

http request -> .htaccess -> index.php -> config

installs most of our environment for us, we will need to duplicate some of them for different input paths.

So, for your script, my first instinct will be to create a shared include file that reflects a lot of what is going on in index.php - sets the inclusion paths, APPLICATION_PATH, creates instances and invokes loading, and then does your script special processing .

Even better, it might be advisable to create a single entry point for all your scripts, for example, in the context of http / web. Extend Zend_Application for your own script purposes to $application->run(); no longer triggered the dispatcher processing of the MVC dispatcher router, but rather your own stuff. Thus, this single script entry point will look almost identical to the web entry point, the only distinguishing feature of which is the application object instance. Then pass the name of the desired application class as a command line parameter to the script.

But here I admit I'm less confident and just throw away ideas.

Hope this helps someone. It actually helped me record all this. Thank you and welcome!

Update 2009-09-29: Just looked at this article: Using the Zend Framework from the command line

Update 2009-11-20: And another article: Cron Tasks in Zend Framework | GS design

Update 2010-02-25: Simple command line scripts with Zend Application - David Caunt

+1
source

All Articles