-
23 Jun 2010 5:38 AM #1
Extremely Easy Ext.Direct integration with PHP
Extremely Easy Ext.Direct integration with PHP
*** Compatible with ExtJS 4 ***
This is an updated / refactored version of "Easy Ext.Direct integration with PHP"
How to use:
1) PHP
Here, "Server" is the PHP class that we want to provide access from the JavaScript code. It could be any other class.PHP Code:<?php
require 'ExtDirect.php';
class Server
{
public function date( $format )
{
return date( $format );
}
}
ExtDirect::provide( 'Server' );
?>
2) HTML:
Here, "ext-direct.php" points to the PHP file shown on item 1. The "?javascript" query string is necessary, because the default output is on JSON format (good for Ext Designer).Code:<script type="text/javascript" src="ext-direct.php?javascript"></script>
3) JavaScript:
Here, to call the "date" method from PHP "Server" class, we prepended the default namespace Ext.php. The first parameter is the $format parameter. The second parameter is the JavaScript callback function that will be executed after the AJAX call has been completed. Here, an alert box shows the result.Code:Ext.php.Server.date( 'Y-m-d', function(result){ alert( 'Server date is ' + result ); } );

What are you waiting for the download?
It includes:
ExtDirect.php - This is the file you include in your PHP script.
example.php - This is a working sample (PHP part).
example.html - The HTML and JavaScript parts of the working sample.
In the next post, I tell you about the features and configuration options.
Thanks.
The current latest version is ExtDirect_2011-11-08.zip, which you can download below:Last edited by j.bruni; 8 Nov 2011 at 5:31 PM. Reason: New version available (November 8, 2011)
-
23 Jun 2010 9:00 AM #2
-
23 Jun 2010 10:33 AM #3
Features
Features
Thanks, Khebs!
You haven't seen nothing yet!
Look at the Features
- API declaration with several classes (not limited to a single class)
- API "url", "namespace" and "descriptor" settings ("ExtDirect" class assigns them automatically if you don't)
- Two types of API output format: "json" (for use with Ext Designer) and "javascript" (default: json)
- You choose if the "len" attribute of the actions will count only the required parameters of the PHP method, or all of them (default: all)
- You choose whether inherited methods will be declared in the API or not (default: no)
- You choose whether static methods will be declared in the API or not (default: no)
- Instantiate an object if the called method is static? You choose! (default: no)
- Call the class constructor with the actions parameters? You choose! (default: no)
- "debug" option to enable server exceptions to be sent in the output of API action results (default: off)
- "utf8_encode" option to automatically apply UTF8 encoding in API action results (default: off)
Learn how to use all these configuration options in the next post. It is so simple!
-
23 Jun 2010 11:03 AM #4
Hey! Forgot to mention in the features list:
- Handles forms
- Handles file uploads
-
23 Jun 2010 11:13 AM #5
Configuration - How To
Configuration - How To
Easy.
If the configuration option name is "configuration_name" and the configuration value is $value, use this syntax:
And that's all.PHP Code:ExtDirect::$configuration_name = $value;
Now, let's see the available configuration options:
- name: api_classes
- type: array of strings
- meaning: Name of the classes to be published to the Ext.Direct API
- default: empty
- comments: This option is overriden if you provide a non-empty $api_classes parameter for the "ExtDirect::provide" method. Choose one or another. If you want to declare a single class, you can set $api_classes as a string, instead of an array.
- example- name: urlPHP Code:ExtDirect::$api_classes = array( 'Server', 'OtherClass', 'StoreProvider' );
- type: string
- meaning: Ext.Direct API attribute "url"
- default: $_SERVER['PHP_SELF']
- comments: Sometimes, PHP_SELF is not what we want. So, it is possible to specify the API URL manually.
- example- name: namespacePHP Code:ExtDirect::$url = '/path/to/my_php_script.php';
- type: string
- meaning: Ext.Direct API attribute "namespace"
- default: "Ext.php"
- comments: Feel free to choose your own namespace, according to ExtJS rules for it.
- example- name: descriptorPHP Code:ExtDirect::$namespace = 'Ext.Dharma';
- type: string
- meaning: Ext.Direct API attribute "descriptor"
- default: "Ext.php.REMOTING_API"
- comments: Feel free to choose your own descriptor, according to ExtJS rules for it, and to the chosen namespace.
- example- name: count_only_required_paramsPHP Code:ExtDirect::$descriptor = 'Ext.Dharma.REMOTING_API';
- type: boolean
- meaning: Set this to true to count only the required parameters of a method for the API "len" attribute
- default: false
- example- name: include_static_methodsPHP Code:ExtDirect::$count_only_required_params = true;
- type: boolean
- meaning: Set this to true to include static methods in the API declaration
- default: false
- example- name: include_inherited_methodsPHP Code:ExtDirect::$include_static_methods = true;
- type: boolean
- meaning: Set this to true to include inherited methods in the API declaration
- default: false
- example- name: instantiate_staticPHP Code:ExtDirect::$include_inherited_methods = true;
- type: boolean
- meaning: Set this to true to create an object instance of a class even if the method being called is static
- default: false
- example- name: constructor_send_paramsPHP Code:ExtDirect::$instantiate_static = true;
- type: boolean
- meaning: Set this to true to call the action class constructor sending the action parameters to it
- default: false
- example- name: debugPHP Code:ExtDirect::$constructor_send_params = true;
- type: boolean
- meaning: Set this to true to allow exception detailed information in the output
- default: false
- example- name: utf8_encodePHP Code:ExtDirect::$debug = true;
- type: boolean
- meaning: Set this to true to pass all action method call results through utf8_encode function
- default: false
- example- name: default_api_outputPHP Code:ExtDirect::$utf8_encode = true;
- type: string
- meaning: API output format - available options are "json" (good for Ext Designer) and "javascript"
- default: "json"
- comments: Another way to enforce "javascript" output is to append the "?javascript" query string in the end of your PHP script URL; do this in the HTML <script> tag that refers to your API
examplePHP Code:ExtDirect::$default_api_output = "javascript";
-
23 Jun 2010 12:07 PM #6
Yes i was playing with it, very good indeed.. and nice one w/ json & javascript
nothing is much simpler than this.. thanks for sharing! 
-
23 Jun 2010 12:44 PM #7
Thanks J. Saw your response in your other post, skipped over here, and like what I see. Thanks for sharing with everyone. Andy
-
24 Jun 2010 8:50 AM #8
-
25 Jun 2010 3:40 PM #9
1. Download the "ExtDirect.php.zip" file from post #1 of this thread.
2. Unzip the file
3. Copy the extracted files (ExtDirect.php, example.php, and example.html) to your web site folder (requires PHP and and HTTP, like Apache or NginX)
4. Using your browser, go to the URL where you can access "example.html"... it can be "http://localhost/example.com" or something else, according to your settings.
Hope this helps...
-
27 Jun 2010 9:46 AM #10
Hi i think i found a bug on your ExtDirect.php class.. when i use multiple classes i got an error at line: 381, stating that it is passing a string not an array.. so a quick fix is..
@ExtDirect.php, line 381
Classes:Code:if (!is_array($this->parameters)) $this->parameters = array($this->parameters); return call_user_func_array( array( $this->instance, $this->method ), $this->parameters );
>> Cds.php
>> Server.phpCode:<?php class Cds { public function getAll() { $db = Crystal::db(); return $db->get('cds')->fetch_all(); } } ?>
>> api.phpCode:<?php class Server { public function date($timezone = 'Asia/Urumqi') { date_default_timezone_set( $timezone ); return date( 'm/d/Y h:i:s a' ); } } ?>
Code:<?php ## @required php's require 'libraries/ExtDirect.php'; require 'libraries/crystal-0.4/Crystal.php'; require 'classes/Server.php'; require 'classes/Cds.php'; ExtDirect::provide(array('Server','Cds')); ?>
Similar Threads
-
Ext.Direct PHP backend
By ckr in forum Ext.DirectReplies: 34Last Post: 11 Jun 2012, 1:30 PM -
Alternative Ext Direct PHP Implementation
By TommyMaintz in forum Ext.DirectReplies: 36Last Post: 26 Sep 2011, 12:08 AM -
Easy Ext.Direct integration with PHP
By j.bruni in forum Ext.DirectReplies: 2Last Post: 23 Jun 2010, 11:27 AM -
Simple Ext.Direct PHP apps
By pkristiana in forum Community DiscussionReplies: 1Last Post: 11 Feb 2010, 1:12 AM


Reply With Quote