Friday, April 23, 2010

CodeIgniter using controller for beginner

You must know that are

-config : Configuration files
-helper : helper for application, contain function that you create
-libraries : Collection of your code.
-errors : Contains template for error display
-hooks : Controlling files load.
-model : Collection of your code
-controller : Controller, control client request to others(view,library... etc)
-view : Template for showing information to user.



simple create test page

1. if your class name is thegreatestpost, you should save your controller file as thegreatestpost.php

<?php
class thegreatestpost extends Controller {

function index()
{
echo 'Hello the greatest post!';
}
}
?>


to view it, www.your-site.com/index.php/thegreatestpost/


if you want controller thegreatestpost is default controller, just edit $route['default_controller'] in config\routes.php as :
$route['default_controller'] = 'thegreatestpost';
and url to view index is www.your-site.com/index.php/

display result will be:
Hello the greatest post!

Add another function,

<?php
class thegreatestpost extends Controller {

function index()
{
echo 'Hello the greatest post!';
}
function greatest()
{
echo 'Hello postmen';
}

}
?>


to view greatest www.your-site.com/index.php/thegreatestpost/greatest/
result view:
Hello postmen

to passing value url to your function simply like this:

function greatest($value1 = "") : url= www.your-site.com/index.php/thegreatestpost/greatest/123

meaning $value1 = 123

function greatest($value1 = "", $value2 = "") :url= www.your-site.com/index.php/thegreatestpost/greatest/123/abc

meaning $value1 = 123, $value2 = abc


more reference visit : http://codeigniter.com/

No comments: