June 23, 2010 UPDATE:
This version is now deprecated.
The code has been refactored, and a new version has just been launched in a new thread:
EXTremely-Easy-Ext.Direct-integration-with-PHP
Now, it is really really easy - I'd say it couldn't be easier.
And, also, full of new features and configurations!
- Ext Designer ready
- Form and file upload handling
- Many classes can be declared in the API
- etc...
From here below, the "old" original post, from three months ago:
Very easy to use.
______________________________________________________________
1) Extend the ExtDirectActions class, like this:
PHP Code:
<?php
require 'ExtDirectApp.php';
class MyMath extends ExtDirectActions
{
public function square( $number )
{
return $number * $number;
}
}
new ExtDirectController( 'MyMath' );
?>
And that's it. This is a fully working sample!
You just need to:
- require "ExtDirectApp.php"
- extend "ExtDirectActions"
- create an "ExtDirectController" instance
The parameter for the Controller constructor must be the name of the API class (i.e., the ExtDirectActions descendant class which will be acessible from the client-side). It is "MyMath" in our example.
Only public methods are included in the API. Protected and private methods are NOT included. Also, only required parameters are counted. Optional parameters (parameters that have a default value) are NOT counted.
Easy! Let's save this file as "app.php"
______________________________________________________________
2) Create a JavaScript template file, like this:
Code:
Ext.Direct.addProvider( { 'url': 'app.php', 'type': 'remoting',
'actions': { 'MyMath': [%actions%] }
} );
Here , you need to:
- use your PHP file path for the "url" (app.php)
- use the API class name in the "actions" object (MyMath)
- use the [%actions%] tag, that will be replaced by the class API definition
Save this file as "app.js"
______________________________________________________________
3) In an HTML file, include the ExtJS required files as usual, and then include your PHP file as the JavaScript source of a "script" tag, as shown below:
Code:
<script type="text/javascript" src="app.php"></script>
And, believe me! You are ready!!!
______________________________________________________________
You are now able to call your PHP class methods from JavaScript, using a syntax like this:
Code:
MyMath.square( 4, callback );
The "callback" parameter is the callback function that will be executed after the server has delivered the result. The result is the first parameter passed to the callback function. For example:
Code:
callback = function(result) { alert( 'Result returned from server: ' + result ); }
______________________________________________________________
This is the basic usage.
Please, download the "ExtDirectApp.php" file here:
http://www.phpclasses.org/browse/package/6088/
Note that "ExtDirectApp.php" is the only required file.
______________________________________________________________
There are a couple of additional features, that I may elaborate in another post. Let me have some feedback first. Thanks!
