This is a simple demo how to use Zend_AMF with Yii.
Download Zend_AMF and extract library/Zend to protected/vendors/Zend
Info: Zend AMF 1.11.11 has a nasty bug, so use Zend AMF 1.11.10 or Zend AMF 1.11.12 when it's released.
Zend Framework class loader for Yii. Create zend/EZendAutoloader.php to protected/extension:
/** * EZendAutoloader * * @package extensions.zend */ class EZendAutoloader extends CApplicationComponent { /** * @var string Path to the folder that contains Zend/ and the rest of the ZF libraries; optional, * and only useful if you don't already have the folder listed under 'import' in your config file. */ public $basePath; /** * @var array(string,string,...) An array of custom namespaces the Zend_Loader_Autoloader should import, eg. 'My_' */ public $customNamespaces = array(); /** * Set up Zend_Loader_Autoloader to listen for any autoload requests. */ public function init() { $path = $this->basePath; if (strlen($path)) { // Normalise: strip a trailing "/" if present. if (strlen($path) > 1 && $path[strlen($path)-1] == '/') { $path = substr($path, 0, strlen($path)-1); } // Add this library path to PHP's include_path if it's not // already there (common for library paths outside of protected/). $this->addIncludePath($path); // Add a / for ease below. $path .= '/'; } // Get Yii out of the way. spl_autoload_unregister(array('YiiBase','autoload')); // Bring in Zend and set it up to take new autoload events. require_once($path.'Zend/Loader/Autoloader.php'); $autoloader = Zend_Loader_Autoloader::getInstance(); // Any custom namespaces, eg. 'My_' foreach ($this->customNamespaces as $namespace) { $autoloader->registerNamespace($namespace); } // Back to Yii. spl_autoload_register(array('YiiBase','autoload')); } /** * Add include path * @param $path */ protected function addIncludePath($path) { if (!file_exists($path) OR (file_exists($path) && filetype($path) !== 'dir')) { throw new CException(Yii::t('Include path {path} does not exist.', array('path'=>$path))); } $paths = explode(PATH_SEPARATOR, get_include_path()); if (array_search($path, $paths) === false) { array_push($paths, $path); } set_include_path(implode(PATH_SEPARATOR, $paths)); } }
Create a simple Value Object class to protected/components/vo/Application.php
/** * Value Object Class for AMF Service */ class Application { public $time = ''; public $datetime = ''; }
Create a Service class to protected/components/Service.php
/** * Service Class for Zend_AMF */ class Service { public function getTime() { return time(); } public function getDatetime() { return date('Y-m-d H:i:s'); } public function validateEmail($value) { $validator = new CEmailValidator; if(!empty($value) && $validator->validateValue($value)) return 'ok'; return 'error'; } public function appInit() { $a = new Application; $a->time = $this->getTime(); $a->datetime = $this->getDatetime(); return $a; } }
Modify your main config:
// Main Web application configuration. // Preloading 'log' and 'zend' component 'preload'=>array( 'log', 'zend', ), // Autoloading model, component, value object and vendor classes 'import'=>array( 'application.models.*', 'application.components.*', 'application.components.vo.*', 'application.vendors.*', ), // Application components // Application Log 'log'=>array( 'class'=>'CLogRouter', 'routes'=>array( // Save log messages on file array( 'class'=>'CFileLogRoute', 'logFile'=>'web.log', 'levels'=>'error, warning, info', ), array( 'class'=>'CFileLogRoute', 'logFile'=>'web_trace.log', 'levels'=>'trace', ), ), ), // Zend autoloader 'zend'=>array( 'class'=>'ext.zend.EZendAutoloader', // Optional: provide an absolute path to the non-Yii directory containing the Zend libraries. //'basePath'=>realpath(dirname(__FILE__).'/../../../../private/libs/zf-1.11.10'), ),
Create a AmfController (protected/controllers/AmfController.php):
class AmfController extends CController { public function actionIndex() { $server = new Zend_Amf_Server(); // It's recommend to disable production mode only when in development. $server->setProduction(false); // Add our class to Zend AMF Server. $server->setClass("Service"); // Mapping the ActionScript VO to the PHP VO. You don't have to add the package name. $server->setClassMap("VOApplication", "Application"); $handle = $server->handle(); echo $handle; } }
If you are using Environment class for Yii, you can use the 'environment' variable to set the production mode.
// Enable production mode if environment is 'production'. if (Yii::app()->params['environment'] == 'production') { $server->setProduction(true); } else { $server->setProduction(false); }
You can test the AMF gateway with Pinta AMF service test and debug utility.
If you installed the app to http://localhost/flash, then connect to gateway url http://localhost/flash/amf and call getTime, getDatetime or AppInit method with pinta.

Be the first person to leave a comment
Please login to leave your comment.