After scouring the many tutorials & posts following the release of ZendAMF I felt a lot of the examples were somewhat lacking. I couldn’t find a reason to ditch AMFPHP and start using ZendAMF.
Being new to the Zend framework, the bootstrap setup was certainly a lot more complicated than getting a AMFPHP service up and running. I decided stick with it though and found some real gems to help out in the production of my remoting applications. Over the next two posts I’ll build a simple ‘F1 Driver Standings’ application using Zend, Flex & Flash. (Updated to work with Zend 1.9.6)
Requirements:
- Web server running PHP 5.2.4+, MySQL 5, Zend Framework.
Localhost solutions:
Once you’ve downloaded the Zend Framework, extract the contents of the library folder to your server library folder. (You should have a library folder above your web root directory, if not create one). You should now see the ‘Zend’ folder within your library.
Whilst we’ve got Explorer/Finder open lets create the directory structure for our Application. Create a new “includes/apps” folder structure above our webroot – e.g. “Application/MAMP/includes/apps”, this is were we will store all our future AMF applications. I prefer to use the package structure used in ActionScript to identify my applications, so with this in mind create the following folder structure for our app (case-sensitive):
[server path]/includes/apps/Formulaone/config
[server path]/includes/apps/Formulaone/Model
[server path]/includes/apps/Formulaone/Model/Dto
[server path]/[web root directory]/formulaone
(e.g. Applications/MAMP/htdocs/formulaone or c:/wamp/www/htdocs/formulaone)
Zend_Config_Ini
Zend framework features are immediately available to us through the use of the Zend_Config_Ini class and a .ini file, we can use these to store the configuration of our database. Create a new text file and insert the following:
[production]
database.adapter = PDO_MYSQL
database.params.host = [YOUR_SERVER_HOST] // e.g. localhost
database.params.dbname = [YOUR_DATABASE_NAME]
database.params.username = [YOUR_DB_USERNAME]
database.params.password = [YOUR_DB_PASSWORD]
[development : production]
database.params.dbname = [YOUR_DATABASE_NAME]
database.params.username = [YOUR_DB_USERNAME]
database.params.password = [YOUR_DB_PASSWORD]
In this file we are creating settings for a dev & production environment. In this case our development settings will override the production settings. We’re also specifying the database adapter we wish to use in our application. In this case we’ll be using the PDO_MYSQL adapter to connect to our MySQL database. Save the file as “app.ini” in the config folder you’ve just created, ([server path]/includes/apps/Formulaone/config/app.ini).
Bootstrap
Without getting into too much detail, the Bootstrap file is basically the blueprint for our application, through which all requests are routed. There’s a great article here that explains it in more detail. Create your bootstrap file, “index.php”, in your public folder, (http://localhost/formulaone/index.php). Open index.php in your favourite php editor and let’s get coding:
setFallbackAutoloader(true);
// RETRIEVE OUR CONFIG FILE
$config = new Zend_Config_Ini( 'app.ini', 'development' );
$dbAdapter = Zend_Db::factory( $config->database );
Zend_Db_Table_Abstract::setDefaultAdapter( $dbAdapter );
// INITIATE THE AMF SERVER
$server = new Zend_Amf_Server();
$server->setClass('F1DriversService');
$server->setClassMap( 'F1DriverDTO', 'F1DriverDTO' );
$server->setProduction(false);
$response = $server->handle();
echo $response;
// REMOVE ITEMS FROM GLOBAL SCOPE
unset( $dbAdapter, $config );
To begin with we need to set the include paths to the files we’ll be using, including the path to the Zend framework using the set_include_path() method. We then make use of a nice feature of the Zend framework, class auto loading using Zend_Loader. No need for includes in the rest of application – Nice!
require_once 'Zend/Loader/Autoloader.php';
$loader = Zend_Loader_Autoloader::getInstance();
$loader->setFallbackAutoloader(true);
Load in the database config file we created earlier using the Zend_Config_Ini class:
$config = new Zend_Config_Ini( 'app.ini', 'development' );
The Zend_Config_Ini requires we pass in the name of the INI file to load and the section within the INI file to load. In this case we are going to use the development settings. We can now access the database adapter set in our config INI file and make the Zend_Db_Table_Abstract adapter default to PDO_MYSQL:
$dbAdapter = Zend_Db::factory( $config->database );
Zend_Db_Table_Abstract::setDefaultAdapter( $dbAdapter );
unset( $dbAdapter, $config );
From here on, any class extending Zend_Db_Table_Abstract will automatically use the PDO_MYSQL adapter, removing the need to specify this in each model/table class we create.
We now finish up our bootstrap file by creating an instance of the Zend_Amf_Server, set the name of the service class we wish to use and list the PHP classes we wish to map to AS3 equivalents:
$server = new Zend_Amf_Server();
$server->setClass('F1DriversService');
$server->setClassMap( 'F1DriverDTO', 'F1DriverDTO' );
$server->setProduction(false);
$response = $server->handle();
echo $response;
That’s the ground work done. This all seems like a lot to set up, but believe me, it’s worth it in the long run. In part 2, we’ll start building our application.




I find that the default config doesn’t work, I’ll need to try your method