Adobe Flex with PHP using ZendAMF – first steps

Unsurprisingly the support of Adobe technologies that integrate seamlessly with Flex. Zend, which maintains the PHP, have your ZendFramework, a package called ZendAMF to implement such an integration between Flex and PHP. In the hands of Wade Arnold, who also holds the AMFPHP.
If took me a while to start using this implementation, had already gotten used to het AMFPHP and made several systems with it. However, their adobe recommends the use of ZendAMF for building RIAS’s with Flex and PHP.
As a first example using the ZendAMF, i will use the same database meda in the previus article about Flex and Java.
Ona advantage of Flex is designed to be the view layer, and this alllow the business layer is transparent and can user either Java, PHP or .NET, need only pointo to a different endpoint according to their need.
You can download the latest version of ZendAMF the following link. More importantly, you do note need to dowload any ZendFramework to use this packag.
Once that’s done, extract the files in the directory of your choice, in my case, i created one called “ZendAMF” and make sure thath the directory contains “Zend”.
The objective is not to change almost anything int the form of organization which was in AMFPHP. So to test our AMF, create a file called “gateway.php” with the following code:
[PHP]
< ?php require_once 'Zend/Amf/Server.php'; $server = new Zend_Amf_Server(); $server->setProduction(false);
echo($server->handle());
?>
[/PHP]
Running the link in your browser, you should see the following message:

Zend Amf Endpoint

The ZendAMF are ready for our development, i tried to keep the directory structure as close as possible to what they had in java, therefore, create the following directory to our VO; br.com.leonardofranca.vo and br.com.leonardofranca for the main class. The structure should be as follows:

Zend
br/com/leonardofranca/vo
br/com/leonardofranca
gateway.php

Our VO in PHP would be as follows:
[PHP]
< ?php class ContatosVO { public $_explicitType = 'br.com.leonardofranca.vo.ContatosVO'; public $id = 0; public $nome = ""; public $email = ""; function __construct() { } public function getId() { return $this->id;
}

public function setId($id)
{
$this->id = $id;
}

public function getNome()
{
return $this->nome;
}

public function setNome($nome)
{
$this->nome = $nome;
}

public function getEmail()
{
return $this->email;
}

public function setEmail($email)
{
$this->email = $email;
}
}
?>
[/PHP]
Then, our main class, which will perform the operations list, insert, delete and update data from our database:
[PHP]
< ?php /* * author Leonardo França * site http://www.leonardofranca.com.br */ require_once 'vo/ContatosVO.php'; class Contatos { private $conexao; private $sql; private $query; private $num; private $dados; public $contatosVO; function __construct() { $this->conexao = mysql_connect(“localhost”,”root”,””);
mysql_select_db(“java”);
}

public function getData()
{
$this->sql = “SELECT id, nome, email FROM tabela”;
$this->query = mysql_query($this->sql);
$this->num = mysql_num_rows($this->query);
if ($this->num>0)
{
while ($this->row = mysql_fetch_assoc($this->query))
{
$contatosVO = new ContatosVO();
$contatosVO->setNome($this->row[‘nome’]);
$contatosVO->setEmail($this->row[’email’]);
$contatosVO->setId($this->row[‘id’]);
$this->dados[] = $contatosVO;
}
return($this->dados);
}
else
{
return false;
}

}

public function insertData($contatosVO=ContatosVO)
{
$this->contatosVO = $contatosVO;
$this->sql = “INSERT INTO tabela (nome,email) VALUES
(‘”.$this->contatosVO->nome.”‘,
‘”.$this->contatosVO->email.”‘)”;
$this->query = mysql_query($this->sql);
if (!mysql_error())
{
return true;
}
else
{
return false;
}
}

public function deleteData($contatosVO=ContatosVO)
{
$this->contatosVO = $contatosVO;
$this->sql = “DELETE FROM tabela WHERE id = ‘”.$this->contatosVO->id.”‘ LIMIT 1″;
$this->query = mysql_query($this->sql);
if (!mysql_error())
{
return true;
}
else
{
return false;
}
}

public function updateData($contatosVO=ContatosVO)
{
$this->contatosVO = $contatosVO;
$this->sql = “UPDATE tabela SET nome = ‘”.$this->contatosVO->nome.”‘, email = ‘”.$this->contatosVO->email.”‘ WHERE id = ‘”.$this->contatosVO->id.”‘”;
$this->query = mysql_query($this->sql);
if (!mysql_error())
{
return true;
}
else
{
return false;
}
}

}
?>
[/PHP]
Until now, no mystery, we have a simple class that uses a typed object, now let’s edit the file “gateway.php” so you can call our vo and implementation class:
[PHP]
< ?php require_once 'Zend/Amf/Server.php'; require_once 'br/com/leonardofranca/vo/ContatosVO.php'; require_once 'br/com/leonardofranca/Contatos.php'; /** Bootstrap */ // Instantiate server $server = new Zend_Amf_Server(); $server->setProduction(false);
//$server->addDirectory(‘services/’);
// reflection
$server->setClass(‘Contatos’);
$server->setClassMap(‘ContatosVO’,”br.com.leonardofranca.vo.ContatosVO”);
// Handle request
echo($server->handle());
?>
[/PHP]
Added the call our implementation class and mapped the vo, done that, let’s the hardest part now :P,
got the same mxml file of the previous articles and modify the following line:
[MXML]
< mx : RemoteObject id="ro" destination="zendamf" source="br.com.leonardofranca.Contatos" endpoint="http://localhost/ZendAmf/gateway.php">

[/MXML]
Moreover, i did not change anything in programming in Fle, the objective here was to show a simple way, how we can have a view layer rich with Adobe Flex, regardless of the business layer.

Mais:
http://wadearnold.com/blog/?page_id=155
http://framework.zend.com/download/amf
http://framework.zend.com/wiki/display/ZFPROP/Zend_Amf+-+Wade+Arnold

UPDATE

In later versions of ZendAmf, you nedd some packages contained in ZendFramework, one option is to go adding the packages in your project or use any ZendFramework.

Recommended book:

Translations:
Português do Brasil

Was this article helpful? feel free to make a donation and help keep the blog in the air
Flex, PHP, ZendAMF , ,

1 comment


  1. Pingback: Leonardo França » Adobe Flex com PHP usando ZendAMF – primeiros passos

Leave a Reply