PDA

View Full Version : integration with codeigniter



italoc
4 Feb 2008, 9:02 AM
it is possible to integrete codigniter with extjs?

where i can find a tutorial?

gregsci
6 Feb 2008, 7:34 PM
Hi! I managed to integrate codeigniter with ext 2.0 in the project I'm currently working. I did this and it worked very well:

1- I changed the following config int the file in system/application/config/config.php (codeigniter):
$config['enable_query_strings'] = TRUE;
with this you enable normal query strings in the urls, like index.php?c=controller&m=method
(this was just a design choise, but I guess the urls that codeigniter uses like index.php/controller/method still works with ext, I just dont feel confortable with this kind of url)

2- Create a controller in codeigniter, and in the index() function just load the view that have all the EXT code. In this same controller you can add php functions that will return data through ajax to the view loaded (you can do this in another controller, but rather keep it in the same controller to do things more clear). BTW, name the controller and the view with the same name, this can help clear things. For example:

system/application/controllers/login.php


class Login extends Controller {
function Login(){
parent::Controller();
}

function index(){
//..do things, like load languages files, some data (NO AJAX) etc
$this->load->view('login_view');
}

function ajax_validateLogin() {
$user = $_POST['user'];
$pass = $_POST['pass'];

$this->load->database(); //loading codeigniter's DB class

//connect to db, validate the user in the DB and echo some JSON response
//I used JSON because it was more confortable to my project
//YOU DONT NEED TO LOAD A VIEW IN THE AJAX FUNCTIONS!!! just echo the
//response, this increase efficiency

if($user_valid) {
echo "{success:true}";
} else {
echo "{success:false,msg:'the username and pass are incorrect!'}";
}
}
}


That should be all the controller in our example, and now the view:

system/application/views/login_view.php


var form_login = new Ext.FormPanel({
//...some config
//IMPORTANT! this must be a valid CODEIGNITER URL, indicating the
//controller that have the ajax functions (in our case, the same
//controller that loaded this view) and the method to execute
url:'index.php?c=login&m=ajax_validateLogin',
method:'POST',
items: [
{xtype:'textfield', name:'user', id:'user'},
{xtype:'textfield', name:'pass', id:'pass' ,inputType:'password'}
],
buttons: [{ text: 'Continue', handler:formSubmit }]
});

//the form submit handler
var formSubmit = function() {
form_login.getForm().submit({
success: //your success handler
failure: //your failure handler
});
};


You call this page like: index.php?c=login or index.php?c=login&m=index

And that's all, remeber that EXT forms parses JSON by default, so its easy that your functions in the codeigniter controllers echoes valid json objects directly (dont need to load any other views).

So, resuming, create your codeigniter's controllers and the index() method in the controllers should only load a view that have ALL the EXT code, use valid codeigniter URL in the ajax request in your EXT code, this URL should be a controller and the method that will process the request and return a valid json response, like I said, the controller that loaded the view should have all the functions that this EXT code calls (this to keep things clear, and not creating so many controllers). The functions that proccesses ajax requests in codeigniter controllers SHOULD NOT LOAD any views, just echo the response directly.

Hope I helped you with this!!!
(sorry for my bad english, i speak spanish!)

italoc
7 Feb 2008, 2:11 AM
i have this problem:

- in login_view i need to include the .js library of th EXTJS? if i include the libriary i receive this error:

Ext is not defined
[Break on this error] var form_login = new Ext.FormPanel({

can you help me??

thanks..

mjlecomte
7 Feb 2008, 4:17 AM
did you try searching the forums? There are a few threads about this in the ext forums and also over in the codeigniter forums.

gregsci
9 Feb 2008, 5:31 AM
Of course! you must include all the EXT libraries in the views! But they are not necessary in the controllers, the controllers just handles all php code, and the views all the EXT code.

wachid
28 Jul 2008, 10:01 PM
maybe you should look to this one : http://faqlist.net/2008/07/about-code-igniter-extjs-integration-2.html

and also the example application in here : http://faqlist.net/2008/08/code-igniter-extjs-integration-3.html

cherss

-wachid-

phpeter
28 Dec 2008, 10:54 AM
list($_SERVER['REQUEST_URI'])=explode('&',$_SERVER['REQUEST_URI']);
list($_SERVER['QUERY_STRING'])=explode('&',$_SERVER['QUERY_STRING']);


somewhere in the index.php before



require_once BASEPATH.'codeigniter/CodeIgniter'.EXT;