PDA

View Full Version : Ext.Direct for CodeIgniter



goachka
1 Sep 2009, 1:05 AM
Usage
Just copy archive contents to your application folder.
It will create cache folder in your application folder (make this folder writable!).
It will place extdapi.php, extdcacheprovider.php, extdrouter.php to the libraries folder.
It will place example classes Echo.php, Exception.php, File.php, Time.php to the same folder.
And it will place direct.php to the controllers folder.

Now you can test it:
1. Add a script tag in your main application page for the API:

<script type="text/javascript" src="<?=base_path()?>direct/api"></script>2. Add provider:

Ext.Direct.addProvider(Ext.app.REMOTING_API);3. Now you can call some method, for example Time.get():

Ext.mits.Time.get(function(provider, response) {
Ext.Msg.alert('response', response.result);
});Now you can use your own classes:
Place them to the libraries folder. Don't forget to make comments before each method of your class. First of all it should have @remotable attribute:

class Time {
/**
* @remotable
*/
public function get(){
return date('m-d-Y H:i:s');
}
}If you use method to submit form, add @formHandler attribute:

/**
* @remotable
* @formHandler
*/Just see example classes, included in this implementation.

To include your own classes replace $api definition in file direct.php with your own:

$api->add(
array(
'MyClass1',
'MyClass2',
...
)
);Classes files should be named accordingly MyClass1.php, MyClass2.php, ...

If you want to use CodeIgniter object in your classes, just get instance:

$CI =& get_instance();

P.S. This code doesn't work on Windows platform, because of backslashes in win paths.. something should be updated...

khebs@live.com
6 Nov 2009, 3:17 AM
hi goachka, thanks for this dude, but, just would like to ask if, does this contribute to any performance using Ext.Direct Techniques? and when do we need to use Ext.Direct approach? hope you could make some time buddy... reply much appreciated. ;)

goachka
6 Nov 2009, 3:23 AM
You want to know what is Ext.Direct?


hi goachka, thanks for this dude, but, just would like to ask if, does this contribute to any performance using Ext.Direct Techniques? and when do we need to use Ext.Direct approach? hope you could make some time buddy... reply much appreciated. ;)

khebs@live.com
6 Nov 2009, 5:04 AM
No no.. hmm, im using CodeIgniter too, but i'm using the standard approach, by using Ext.Direct in your point of views, does it contribute to any performance? and when will it be; for a developer to choose Ext.Direct for his/her backend?

goachka
6 Nov 2009, 5:12 AM
it's just the same ;)
i've just made it as CodeIgniter library to load like $this->load->library(...);


No no.. hmm, im using CodeIgniter too, but i'm using the standard approach, by using Ext.Direct in your point of views, does it contribute to any performance? and when will it be; for a developer to choose Ext.Direct for his/her backend?

khebs@live.com
6 Nov 2009, 5:18 AM
ahahaha! ok.. anyway, nice library buddy.. thanks.. saves me some time..

cherbert
24 Feb 2010, 8:18 AM
I am a bit confused as I have never used Libraries before. I generally use the Model, View, Controller featrues.

Can you provide me a quick real world example of how for instance you would use CI's normal Database function to create a select query and then send the json result? Would I need to ditch using Models for this and start creating separate classes in the Library folder?

Just a nudge would help to so I can get my head round how things should be structured.

cherbert
24 Feb 2010, 10:18 AM
Well I can't get this working at all. In Ext I'm getting 'provider is undefined'.

To troubleshoot I pointed my browser at the direct controller mysite/direct/api and I am getting the following PHP Notice.

Undefined variable: api

I have everything installed as per the instructions. libs all in the libraries folder and direct.php in the controller folder. I have created the cache folder and it is writable.

goachka
25 Feb 2010, 12:52 AM
Well I can't get this working at all. In Ext I'm getting 'provider is undefined'.

To troubleshoot I pointed my browser at the direct controller mysite/direct/api and I am getting the following PHP Notice.

Undefined variable: api

I have everything installed as per the instructions. libs all in the libraries folder and direct.php in the controller folder. I have created the cache folder and it is writable.

hmm.... Undefined variable: api - which file? which line?

khebs@live.com
25 Feb 2010, 12:58 AM
in your direct controller, it should be:



$this->extdapi->add(
array(
'Echo' => array('prefix' => 'Class_'),
'Exception' => array('prefix' => 'Class_')
)
);

goachka
25 Feb 2010, 1:02 AM
in your direct controller, it should be:



$this->extdapi->add(
array(
'Echo' => array('prefix' => 'Class_'),
'Exception' => array('prefix' => 'Class_')
)
);


where is the difference?

khebs@live.com
25 Feb 2010, 1:09 AM
goachka, in your zip file its:



<?php

class Direct extends Controller {

function Direct()
{
parent::Controller();
$this->load->library('extdapi');
$this->load->library('extdcacheprovider', array('filePath' => 'cache/api_cache.txt'));
}

function api($output = true)
{
$this->extdapi->setRouterUrl('/direct/router'); // default
$this->extdapi->setCacheProvider($this->extdcacheprovider);
$this->extdapi->setNamespace('Ext.app');
$this->extdapi->setDescriptor('Ext.app.REMOTING_API');
$this->extdapi->setDefaults(array(
'autoInclude' => true,
'basePath' => 'libraries'
));

$api->add(
array(
'Echo' => array('prefix' => 'Class_'),
'Exception' => array('prefix' => 'Class_'),
'Time',
'File'
)
);

if($output) $this->extdapi->output();
$this->session->set_userdata(array('ext-direct-state' => $this->extdapi->getState()));
}

function router()
{
if(!$this->session->userdata('ext-direct-state')) {
$this->api(false);
}
$this->load->library('extdrouter', array('api' => $this->extdapi));
$this->extdrouter->dispatch();
$this->extdrouter->getResponse(true);
}
}

goachka
25 Feb 2010, 1:37 AM
oh! of cause!
strange... i'm using the correct code myself :)
thanks, khebs!
i've updated .zip file

cherbert
25 Feb 2010, 5:44 AM
ok. I now have another issue and I'm tearing my hair out!

Trying to just use the demo classes. Ext is firing off to the router ok with action = 'Time' and method = 'get'



{"action":"Time","method":"get","data":null,"type":"rpc","tid":2}



The response code coming back from the router is..



{"type":"exception","tid":2,"message":"Call to undefined class: Time","where":"#0 \/data\/www.businessnetwork.co.uk\/system\/application\/libraries\/extdrouter.php(62): ExtDRouter->rpc(Object(stdClass))\n#1 \/data\/www.businessnetwork.co.uk\/system\/application\/controllers\/direct.php(90): ExtDRouter->dispatch()\n#2 \/data\/www.businessnetwork.co.uk\/system\/codeigniter\/CodeIgniter.php(236): Direct->router()\n#3 \/data\/www.businessnetwork.co.uk\/index.php(228): require_once('\/data\/www.busin...')\n#4 {main}"}



I have all the demo classes in my libraries folder. And here is my direct.php



class Direct extends Controller {

function Direct()
{
parent::Controller();
$this->load->library('extdapi');
$this->load->library('session');

$this->load->library('extdcacheprovider', array('filePath' => 'cache/api_cache.txt'));
}

function api($output = true)
{
$this->extdapi->setRouterUrl('/direct/router'); // default
$this->extdapi->setCacheProvider($this->extdcacheprovider);
$this->extdapi->setNamespace('Application');
$this->extdapi->setDescriptor('Application.REMOTING_API');
$this->extdapi->setDefaults(array(
'autoInclude' => true,
'basePath' => 'libraries'
));

$this->extdapi->add(
array(
'Echo' => array('prefix' => 'Class_'),
'Exception' => array('prefix' => 'Class_'),
'Time',
'File'
)
);

if($output) {
$this->extdapi->output();
}
$this->session->set_userdata(array('ext-direct-state' => $this->extdapi->getState()));
}

function router()
{
if(!$this->session->userdata('ext-direct-state')) {
$this->api(false);
}
$this->load->library('extdrouter', array('api' => $this->extdapi));
$this->extdrouter->dispatch();
$this->extdrouter->getResponse(true);
}
}



Any suggestions please?

rahulswarnkar
25 Feb 2010, 8:12 AM
Hi goachka, Many thanks for this :) I had the same "Undefined variable: api" problem initially, but figured that out somehow.


P.S. This code doesn't work on Windows platform, because of backslashes in win paths.. something should be updated...

Mine is working on Windows :-?

goachka
25 Feb 2010, 8:23 AM
Hi goachka, Many thanks for this :) I had the same "Undefined variable: api" problem initially, but figured that out somehow.



Mine is working on Windows :-?

You can comment those lines:
33 $this->session->set_userdata(array('ext-direct-state' => $this->extdapi->getState()));
38 if(!$this->session->userdata('ext-direct-state')) {
40 }

This is not good, but it will work. Data will not be stored in session variable and will be generated every time direct/router is called.
I will try to correct code for windows soon, so it will work with sessions.

rahulswarnkar
25 Feb 2010, 8:23 AM
Oh yes, of course I had to make the following change in direct.php


$this->extdapi->setRouterUrl('direct/router'); // defaultto

$this->extdapi->setRouterUrl(site_url('direct/router')); // default

rahulswarnkar
25 Feb 2010, 8:35 AM
You can comment those lines:
33 $this->session->set_userdata(array('ext-direct-state' => $this->extdapi->getState()));
38 if(!$this->session->userdata('ext-direct-state')) {
40 }

This is not good, but it will work. Data will not be stored in session variable and will be generated every time direct/router is called.
I will try to correct code for windows soon, so it will work with sessions.
Thanks goachka, awaiting your updated code =P~

Julio Betta
29 Mar 2010, 10:50 AM
hey guys..
I've tested the example classes inside the libraries folder and everything worked great..
however, I need to set 'controllers' folder as default basePath .. but when a call a method, every time it throws an exception.


"Call to undefined class: Time"
here is my direct.php



class Direct extends Controller {

function Direct()
{
parent::Controller();
$this->load->library('extdapi');
$this->load->library('extdcacheprovider', array('filePath' => 'cache/api_cache.txt'));
}

function api($output = true)
{
$this->extdapi->setRouterUrl(site_url('direct/router')); // default
$this->extdapi->setCacheProvider($this->extdcacheprovider);
$this->extdapi->setNamespace('Application');
$this->extdapi->setDescriptor('Application.REMOTING_API');
$this->extdapi->setDefaults(array(
'autoInclude' => true,
'basePath' => 'controllers'
));

$this->extdapi->add(
array(
'Syndrome',
'Time'
)
);

if($output) $this->extdapi->output();
$this->session->set_userdata(array('ext-direct-state' => $this->extdapi->getState()));
}

function router()
{
if(!$this->session->userdata('ext-direct-state')) {
$this->api(false);
}
$this->load->library('extdrouter', array('api' => $this->extdapi));
$this->extdrouter->dispatch();
$this->extdrouter->getResponse(true);
}
}
PS. the file Time.php is in 'controllers' folder.

goachka
29 Mar 2010, 11:31 PM
Time.php should be in the libraries folder

sommai.k
19 Apr 2010, 4:05 AM
I change some code for support compress feature. At class ExtDAPI method _print line 232 - 244.

Code:


function _print($api) {
$CI =& get_instance();
$CI->output->set_header('Content-Type: text/javascript');

$ret = ($this->_namespace ?
'Ext.ns(\'' . substr($this->_descriptor, 0, strrpos($this->_descriptor, '.')) . '\'); ' . $this->_descriptor:
'Ext.ns(\'Ext.app\'); ' . 'Ext.app.REMOTING_API'
);
$ret = $ret.' = ';
$ret = $ret.json_encode($api);
$ret = $ret.';';
$CI->output->set_output($ret);
}

zcool
23 Apr 2010, 4:07 PM
I'm traing to make an upload using the CI Upload library and Ext Direct and it dosen't works. Could someone put me an example about this?, both parts (JS and PHP) please.

Dumbledore
21 May 2010, 1:23 AM
i reading the first post and i am new to codeigniter... What does basePath() mean? This run into an error.
A complete Sample-Code were great!


[Edit]:

base_path() is not a function. For me it must be <?=$this->config->item('base_url') .$this->config->item('index_page');?>

siberian
9 Jul 2010, 3:42 PM
I am trying to access the user session from C.I. within the Ext.Direct API function

eg:

$perms = $this->session->userdata('perms');

But C.I. tells me:

Severity: Notice --> Undefined property: SponsorAPI::$session

Does anyone have a recipe for accessing the session?

In a controller context the session works fine, its only at this next level down that it fails.

Tx!

Julio Betta
9 Jul 2010, 4:11 PM
try this..



$CI = get_instance();
$CI->load->library('session');
$perms = $CI->session->userdata('perms');

siberian
9 Jul 2010, 7:47 PM
Worked like a champ, tx!

tfcoding
26 Jul 2010, 3:03 PM
Hmm i have read the Thread more than 2 Times, and i dont get into it, some Help would be very nice, first of all the base_path() thing did not work for me, ok i solved this with the base_url, the trouble starts now with the not reachable direct/router method.

Is there anybody who get ext direct working with ci, and could give me some help.

that would be very nice ;)

siberian
26 Jul 2010, 3:21 PM
You will need to be more specific i think. Can you post the error your getting, your calling code and your direct controller?

Tx!
j


Hmm i have read the Thread more than 2 Times, and i dont get into it, some Help would be very nice, first of all the base_path() thing did not work for me, ok i solved this with the base_url, the trouble starts now with the not reachable direct/router method.

Is there anybody who get ext direct working with ci, and could give me some help.

that would be very nice ;)

tfcoding
26 Jul 2010, 3:46 PM
I have Setup the the scripts in a way, i normaly do it,


<link rel="stylesheet" type="text/css" href="<?=base_url().'extJS/resources/css/ext-all.css'?>" />

<script type="text/javascript" src="<?=base_url().'extJS/adapter/ext/ext-base.js'?>"></script>
<script type="text/javascript" src="<?=base_url().'extJS/ext-all.js'?>"></script>
<script type="text/javascript" src="<?=base_url()?>index.php/direct/api"></script>

If i now call my default Controller i get the error

http://www.domain.tld/direct/router" 404 Error,

that seems pretty clear to me, but i dont get right into it, because it seems i am the only one with this error...

The right URL must be http://www.domain.tld/index.php/direct/router", i have done a small worakround to test the functionality of ext direct, with a htaccess file and cutting the index.php away, but i get a response , similar to this one
{"action":"Time","method":"get","data":null,"type":"rpc","tid":2}

I have tried to figure this behaviour out but now i am at a point where i can't think about this problem anymore :-P

siberian
26 Jul 2010, 7:14 PM
What does your apache error log say?

tfcoding
26 Jul 2010, 11:50 PM
Nothing, about this problem

e-mike
14 Sep 2010, 7:38 AM
I had to swap the lines 32 and 33 in direct.php:

if($output) $this->extdapi->output();
$this->session->set_userdata(array('ext-direct-state' => $this->extdapi->getState()));

to:

$this->session->set_userdata(array('ext-direct-state' => $this->extdapi->getState()));
if($output) $this->extdapi->output();

Because if output is TRUE (default) then there is already a header send:

r. 233 extdapi.php header('Content-Type: text/javascript');

(I use the DB to store my sessions and it seems when setting userdata the header gets changed)

e-mike
15 Sep 2010, 3:30 PM
@cherbert: did you solve your problem? I have the same now... ;-(

e-mike
15 Sep 2010, 4:06 PM
OK, it took me a while but the problem described by cherbert is easy to fix.
When the api is called (on the page) the state is made and set (session).
But if some ajax magic happens the api is not called (which is good) and
the set state by the api is not used cousin errors... So get the set state...
The part that there is no set state then run api first was already there...

controllers/Direct.php



public function router()
{
$state = $this->session->userdata('ext-direct-state');
if ( ! $state) $this->api(FALSE);
else $this->ext_direct_api->setState($state);

$this->load->library('ext_direct_router',
array('api' => $this->ext_direct_api));

$this->ext_direct_router->dispatch();
$this->ext_direct_router->getResponse(TRUE);
}

e-mike
16 Sep 2010, 3:40 PM
I found a problem and have a fix (which took me again a while).
If you want to use @formHandler you will keep getting errors...

libraries/extdapi.php r.55



$this->setFormAttribute($state['nameAttribute']);

// must be

$this->setNameAttribute($state['nameAttribute']);
YES YES YES now all three ext direct examples work!!!

change:

<script type="text/javascript" src="php/api.php"></script>

to:

<script type="text/javascript" src="/direct/api"></script>

(this is the controller file at: controllers/Direct.php)

And transform the two files Profile and TestAction in classes
the way Echo, File, Time etc. is done.

e-mike
16 Sep 2010, 4:29 PM
For those who are interested and want to fork the code, see:

BitBucket: http://bit.ly/ckm19M
(https://bitbucket.org/mikebranderhorst/codeigniter-ext-direct)

Gueifu
23 Jan 2011, 11:24 PM
I am going to look at both your suggestions and look for any good deals on these. Thanks again for the help.

ReLexEd
29 Apr 2011, 3:58 AM
If anyone wants to use this under CodeIgniter 2.0, all you have to do is the following:

ChangeIn controller/direct.php:

class Direct extends Controller {

function Direct()
{
parent::Controller();
$this->load->library('extdapi');
$this->load->library('extdcacheprovider', array('filePath' => 'cache/api_cache.txt'));
}


Into:


class Direct extends CI_Controller {

function __construct()
{
parent::__construct();
$this->load->library('ext.direct/extdapi');
$this->load->library('ext.direct/extdcacheprovider', array('filePath' => 'cache/api_cache.txt'));
}

RoyN
4 Jul 2011, 7:32 AM
Is this or any other library been updated?

I wanted to use CI + Ext.Direct and having trouble with CI 2.0.2...

Is anyone using this? Mind to share latest version? I even tried latest download from Mike's link...problems arise... (namely I got stuck trying to get ext.designer to validate the api url - i assume it is because it was returning JS instead of pure JSON)

vadimv
23 Aug 2011, 10:44 PM
I'm trying Mike's library, but I've got stuck and wonder whether anyone can help me. I see actions in provider, but when trying to use them I can't , because actions namespaces are not included in global namespace. Any suggestion would be greatly appreciated.

vadimv
24 Aug 2011, 11:45 PM
found it, actions were namespaced not in Global but in Ext.app, now works.

predator
17 Mar 2012, 3:08 PM
Hi All,

It appears to me that ($this->session->set_userdata(array('ext-direct-state' => $this->extdapi->getState()));)
direct.php


function api($output = true) {
$this->extdapi->setRouterUrl('/direct/router'); // default
$this->extdapi->setCacheProvider($this->extdcacheprovider);
$this->extdapi->setNamespace('Ext.rpc');
$this->extdapi->setDescriptor('Ext.rpc.REMOTING_API');
$this->extdapi->setDefaults(array(
'autoInclude' => true,
'basePath' => 'controllers'
));

$this->extdapi->add(
array(
'Applicants'
)
);

if ($output)
$this->extdapi->output();
$this->session->set_userdata(array('ext-direct-state' => $this->extdapi->getState()));
}


Is overwriting whatever you have stored in the CI session earlier, thus making it impossible to use the CI session for anything else than ext direct.

I have some issues making user authorization work.. after the first ajax call the session does not contain my userdata anymore..

Anybody else having some issue like this??

Thanks

anwar31dec
12 Jun 2012, 2:27 AM
P.S. This code doesn't work on Windows platform, because of backslashes in win paths.. something should be updated..

How can i make it working in window platform, please mention specificly where backslashes should be removed or should be edited. I have done everything with this attached code, i saw the api is woking but when i call the function

Ext.direct.Manager.addProvider(Ext.app.REMOTING_API); Time.get(function(provider, response) {
Ext.Msg.alert('response', response.result);
});

it returns "Time is not defined".

Can anyone give me the complete solution.

saip92
24 Nov 2012, 4:05 AM
What is Ext.mits ?

ngoinhaxinh2013
26 Nov 2012, 6:56 PM
post very in or

predator
9 Feb 2013, 4:56 PM
This was bugging me for quite some time... it was messing up form uploads...

file: exdtapi.php

This:


if (isset($state['nameAttribute'])) { $this->setFormAttribute($state['nameAttribute']); }

Should become:


if (isset($state['nameAttribute'])) { $this->setNameAttribute($state['nameAttribute']); }

Not sure if you guys already fixed that.. maybe I am using old code.

/P

predator
9 Feb 2013, 5:03 PM
Also,
consider
to serialize the data before putting in ci session to avoid the mess.

Like this:


$this->session->set_userdata('ext-direct-state', serialize($this->extdapi->getState()));