AMF existed in ActionScript 2 and was just called AMF as of ActionScript 3 the AMF protocol has been updated and is referred to as AMF3. For historical reasons the original AMF format is now referred to as AMF0. One of the main upgrades to AMF3 is that the object is now zlib compressed for faster transfer do to the smaller file size and the additional of data types that were released with actionscript 3. The architectural role of Flex/Flash/Air in an MCV architecture is of the View. We need to assume that the server has more resources than the client. We can not explicitly trust the client. We assume that the view of the client will change in the future. With these assumptions in mind it is important that the end service that Zend_Amf provides is that of a controller. The controller may need to format data to prepare it for its presentation in the view and will also sanitize any incoming data and authorize the request. If you are use to working with php you should think of the Flash Player application like you would think of a smarty template or Zend_View.
Proposed Component Name
1.0 - 1 January 2008: Initial Draft. (wiki revision: 35)
AMF existed in ActionScript 2 and was just called AMF as of ActionScript 3 the AMF protocol has been updated and is referred to as AMF3. For historical reasons the original AMF format is now referred to as AMF0. One of the main upgrades to AMF3 is that the object is now zlib compressed for faster transfer do to the smaller file size and the additional of data types that were released with actionscript 3.
The architectural role of Flex/Flash/Air in an MCV architecture is of the View. We need to assume that the server has more resources than the client. We can not explicitly trust the client. We assume that the view of the client will change in the future. With these assumptions in mind it is important that the end service that Zend_Amf provides is that of a controller. The controller may need to format data to prepare it for its presentation in the view and will also sanitize any incoming data and authorize the request. If you are use to working with php you should think of the Flash Player application like you would think of a smarty template or Zend_View.
**PHP5 Reflection extension
**Zend_Amf_Value family of classes
Zend_Amf_Server is composed of several components, ranging from the server itself to request, response, encoding, decoding, and fault objects.
To implement Zend_Amf_Server, the developer must attach one or more classes or functions to the server instantiation, via the setClass() and addFunction() methods.
The Zend_Amf_Server will decode request objects that are received via php://input and deserialized the request and check for an existing instantiated session. Zend_Amf_Request will check the AMF header for the matching name space of the requested remote object to be called. The remote class will be instantiated and the result of the method call will be serialized via Zend_Amf_Vaslue and returned to the requested http connection via Zend_Amf_Response.
The following mapping defines what the resulting data type would be when sent from ActionScript to PHP and PHP to actionscript. If a data type that is not specified in this grid is sent to or from Zend_amf it will be handle as a generic object. You can then type cast the object if necessary.
|->||ActionScript to PHP only|
|<-||PHP to ActionScript only|
|<->||bi-directional type mapping|
|RemoteClass||<->||class mapped object|
The following is used to showcase passing an array to and from the Zend Framework. It is not a tutorial for how data
From ActionScript we have a RemoteObject called myservice that's endpoint is Zend_Amf (use case below). We are calling a remote method called sortArray which takes an array as an argument.
In PHP we create a simple class that just re-orders the array and return an array. This is showing how the data types are handled. Zend_AMF hides all serialization from the end user on both sides. This allows the client and server application developers to leverage all of there existing capabilities and does not inject a new set of rules to make the php class Zend_AMF specific.
The result is returned to ActionScript through its callback handler. There is no type handling the method just receives an array.
Getting started with Zend_Amf requires a basic installation of the Zend Framework on your server. Review the getting started guide for setting up your webserver. http://framework.zend.com/manual/en/introduction.installation.html
For a reference we are going to assume that your web server folder is setup in the following manor.
/public or /htdocs or /www /include/
Where the Zend Framework is placed into the /library directory
You will then need to create an index.php where all of your AMF calls will be handled. For this example all calls to the Zend Framework are from an application running in the flash player. If you are using Flash and html views together you will need to create a gateway file independent of the index.php page that has the same content.
The following is an example of your index.php file that will act as the endpoint for AMF calls. The following code instantiates the Zend_Amf server class. You then tell the class what remote services or names spaces that you want to allow your Flasher Player application to access. In the following example we have specified the HelloWorld class to be added into the Zend_Amf server to be remotely exposed. With your webserver properly configured the following file should be accessible at http://localhost/index.php
There is nothing special to Zend_Amf for creating service classes. It is just a standard PHP class file. The following class HelloWorld has one public function say which takes in a string and returns a string that has been concatenated with today's date. Note that private methods will not be exposed through Zend_Amf. Place the file into /include/services/HelloWorld.php
We will use Flex to connect to the application. The following code creates a RemoteObject endpoint that terminates at http://localhost/ which is where our following example is located. The code creates a simple input field and a button that send that data to the server on the click event.
Start with a new flex project from inside of flex builder and call it zend. You should now have an open project called product in your Navigator window. Right click on the zend project name and select 'properties'. In the Project properties dialog go into 'Flex Build Path' menu, 'Library path' tab and be sure the 'rpc.swc' file is added to your projects path. Press Ok to close the window.
We now need to tell Flex which services configuration file to use for inspecting our remote methods. For this reason create a new 'services-config.xml' file into your Flex project root folder. To do this right click on the zend project folder and select 'new' 'File' which will popup a new window. Select the product folder and then name the file 'services-config.xml' and press finish.
Flex has created the new services-config.xml and has it open. Use the following example text for your services-config.xml file. Make sure that you update your endpoint to match that of your testing server. Make sure you save the file.
Now open your project properties panel again by right clicking on the project folder from your Navigator and selecting properties. From the properties popup select 'Flex Compiler' and add the string: -services "services-config.xml". Press Apply then OK to return to update the option. What you have just done is told the Flex compiler to look to the services-config.xml file for runtime variables that will be used by the RemotingService class.
The following code is just in MXML and you can add it to your default application. Run the code and you will remoting with the Zend Framework via Zend_Amf.
It should be noted that additional examples should be constructed that will properly utilize an MVC architecture and folder names. A good database example would be a great next step.