Zend AMF – AS3 and PHP

I know this sounds pretty complicated. It is not, believe me. I will demonstrate in this article how to, simply, connect both technologies together.

Here is a list of tools that I will be using:

For people who don’t have those programs installed, I recommend trying out their trial versions.

Initial Setup

Creating a new Zend Framework project in Zend Studio:

Go to File -> New -> Zend Framework Project.

Let’s enter our project information. As a project name, I typed in “Zend AMF Tutorial”. It can be anything, it doesn’t matter, it is up to you.

For the Project contents, I’m not a big fan of keeping my files hidden in a “workspace” directory, so I chose my own. Uncheck “Use default” and put in the directory of your choice.

Please don’t mind the “Zend Framework Version” section, it is not important.

Once you are done setting up the project path and name, hit the “Finish” button.

By default, Zend Studio creates an MVC layout for all Zend Framework projects.

Now, unzip the Zend Framework archive into the “library” directory under your new project. You have to make sure that you keep the “Zend” directory. The whole Zend Framework depends on it.

We are now ready to write our PHP code!

Now, the whole reason why we want to use the Zend Framework with AMF is because this is the only PHP AMF library that is supported by both Adobe and Zend. There are multiple other AMF libraries out there, but none of them are supported.

As I go, I will give explanations on what I’m doing, to make sure everyone can understand. The purpose of this tutorial is to help you to understand the technology so you can develop any AMF bridge that will suit your needs.

Let’s take a look at the directory structure generated by Zend Studio.

  • application: This folder contains all the PHP code relevant to your application. It contains the MVC components, models, views and controllers.
  • library: This is where the Zend Framework files were copied to. The application has this directory into its include path.
  • public: This is the exposed file structure. It’s your front end. All CSS, JavaScript, images and such goes in there. For the purpose of this example, this will not be used.

Open up the application->default directory. The “default” directory means this is your default application. You can add other applications to the “application” directory if you’d like,  you can include them in the Initializer.php file, adding them to your include path. In the default directory, go to application->default->layouts and edit “main.phtml”.

Replace the whole content with this:

<?= $this->layout()->content; ?>

Save the file.

Now, go to application->default->views->scripts->index and edit “index.phtml”. Simply remove everything and save the file.

The reason why we change those two files is because AMF needs a clean output. The web server cannot output anything else than the AMF data, otherwise ActionScript will give you an error.

Now that our templates are clean, let’s start writing some PHP code.

Go to application->default->controllers and edit “IndexController.php”.

Here is what your file should look like:

class IndexController extends Zend_Controller_Action
    public function indexAction()

* Note that there are no PHP closing tag. This is on purpose. It is Zend’s recommendation to leave the PHP tag open at the end of a class file. The reason for this is if you have to send headers and you have, by mistake, some blank lines at the end of a file, after your closing tag, you will get an error saying that the headers have already been sent. It doesn’t hurt anything to leave the closing tag out.

Let’s put some action into that indexAction method. This is where we declare the Zend_Amf_Server:

public function indexAction()
    $server = new Zend_Amf_Server();

What this code does is tell the AMF server to accept calls to the Services class. This means that the methods you add to the Services class will be exposed and available for use in ActionScript.

It is as simple as that. You now have a functional AMF back end. Now, the only thing that matters is what you put into that “Services” class. What I usually do is use that Services class to support all the models I need for my application. The reason behind that is if you want to use any database support through Zend_Db, only one class can extend the default abstract class for Zend_Db_Table_Abstract. Centralizing the services by creating an entry point simplifies that process.

Part 2 – Services classes and proxy


Leave a Reply