Skip to content

Ajax Framework For CodeIgniter

World Wide Web Server edited this page Jul 4, 2012 · 118 revisions

Cjax is a very basic but powerful Ajax Framework that complements Codeigniter.

Download here:

[url=http://code.google.com/p/ajax-framework-for-codeigniter/downloads/list]Download From Google Code[/url]

Cjax separates your ajax controllers from your regular controllers. While your normal controllers reside in application/controllers, your ajax controllers will reside in application/response.

Install

Download and unzip the Ajax Framework package, then just overwrite everything from the zip into your CodeIgniter base installation (will not replace any of your files)

Test

just go to: http://yoursite.com/ajax.php?controller=test&function=test

(replace http://yoursite.com with the base directory where your CodeIgniter installation resides)

To this point you are done, successfully installing and using Cjax, if the test above prints "Ajax View..". You can find this text inside file application/views/test.php, and its controller in application/response/test.php.

Friendly URLS

Full support for friendly URLS.

Friendly URLS are supported, but not required to use Cjax. You may access your controllers in any of the following ways; these examples make use of controller file application/response/test.php.

[code] //ajax.php?controller=$controller&function;=$function ajax.php?controller=test&function=test //ajax.php?$controller/$function ajax.php?test/test //ajax/$controller/$function #This one requires mod_rewrite ajax/test/test [/code]

The last url above, will require you to enable mod_rewrite in your server and rename the accompanied file htaccess.txt File:htaccess.txt to .htacccess

Creating Controllers

Creating controllers works the same way as your regular controllers, except the class name of ajax controller start with the keyword "controller_". For example, if you want to create a controller named test, then your class name would be "controller_test".

Example

application/response/test.php [code]class controller_test extends CI_Controller {
function test()
{

}

} [/code] You may use your ajax controller the same way you would use any other CI controller including the creation of any dependencies.

Passing Parameters

Cjax uses URL query string to pass parameters into the controller by using function parameters. You can pass alphabetic letters as parameter order to your methods inside the controllers. There is more than one way to pass parameters, depending on the way you are using the URL.

Example of passing parameters in followed order.

URL: [code] ajax.php?test/test/arg1/someID // this also applies to mod_rewrite urls such as: ajax/test/test/arg1/someID [/code] The above URL is passing two parameters arg2 and someID, test/test indicates the controller and the method. To access these parameters use function arguments:

Example

application/response/test.php [code]class controller_test extends CI_Controller {
function test($arg1, $arg2)
{
    echo $arg1;
    echo "<br />";
    echo $arg2;
}
//prints  arg1
//        someID

} [/code]

You can pass unlimited number of parameters, and access them through function parameters in the same order they are passed.

Alphabetic order Parameters

A less Organized way to pass parameters that may accept any order within the URL:

URL: [code]ajax.php?controller=test&function=test&a=Hello&b=World&c=Hello World[/code]

The previous URL, makes use of common URL, and passes three parameters to the controller.

This methods accepts any alphabetic letter in respective order to pass parameters. You may pass: a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z, which will be accessible through function parameter. Using this method also allows you to pass any other parameters through outside of the function parameter scope.

Example

application/response/test.php [code]class controller_test extends CI_Controller {
function test($a, $b, $c)
{
    echo $a;
    echo "<br />";
    echo $b;
    echo "<br />";
    echo $c;
}
//prints  Hello
//        World
//        Hello World

} [/code]

Clone this wiki locally