Tag Archives: codeigniter tutorials

Codeigniter Private Functions


Codeigniter private functions : If you want make a function hidden from public access you can make it private which will be served only in its current scope. Which will not accessible in url.


Syntax of Codeigniter private functions

private function _yourfunction(){


}

if you try to access this function in url it will not be available.

http://example.com/Controllername/_yourfunction

Codeigniter Remap Function


Codeigniter Remap Function : Codeigniter Remap Function is used to override the default function called in url.


For example : www.example.com/User/profile where Home is controller and user is method(function) name.

You can override the function name “profile” with other function name in the controller.
Means you can map original function name with your custom name in url.

Codeigniter Remap Function Example

Codeigniter Remap Function
For example if want to map url www.example.com/User/your_custom_name

about_me();// call about_me() function.
     break;
     case 'edu' : 
      $this->education();// call education() function.
     break;
     default :
      $this->index();
     break;

}

}
public function index(){

echo "Welcome to index function."; 

}

public function profile(){
 echo "Welcome to profile function";
}
public function about_me(){
 echo "Hi I am John !";
}
public function education(){
 echo " I am Master in Computer application.";
}


}   
?>                         

1. Example :

Map http://localhost/User/about to the http://localhost/User/about_me

Hit the url

http://localhost/User/about

Where about calls about_me() function.

Demo

Try it »

2. Example :

Map http://localhost/User/edu to the http://localhost/User/education

Hit the url

http://localhost/User/edu

Where edu calls education() function.

Demo

Try it »

Codeigniter set default controller function


Codeigniter set default controller function: Codegniter default controller is defined in routs.php which means your contrlloer will be accessed when someone access your website- for example www.example.com then the default function will be called. Here in this article we are going to explain this with example.


Codeigniter set default controller function Example

Go to config.php/routs.php and add default controller there.


$route['default_controller'] = 'Welcome/index/$1';

For example if i access :


http://localhost

It will automatically call the welcome controller and index function.

http://localhost/Welcome/index

Codeigniter Default Controller


Codeigniter default Controller : Codeigniter default controller is defined in routs.php which means your controller will be accessed when someone access your website.


Codeigniter default controller Example

Go to config.php/routs.php and add default controller there.


$route['default_controller'] = 'Welcome';

For example if i access :


http://localhost

It will automatically call

http://localhost/Welcome

Codeigniter Controller Function And Parameters


Codeigniter controller function and parameters : The codeigniter controller contains the methods inside it. The parameters passed in url along with function name are directly received inside the methods.


Codeigniter controller function And parameters Example

Lets Understand with the following example and demo :

";
   echo "Name: ". $name."
"; } } ?>

1. Call index() function :

To access index() function hit like this :

http://localhost/Home/index

Where index calls index() function.

Demo

Try it »

1. Call profile() function :

To access profile() function hit like this :

http://localhost/Home/profile

Where profile calls function() function.

Demo

Try it »

1. Call comments() function with parameters :

You can define default parameter in function as :

commnts($id = null, $name = null)

Access the comments function with parameter :

http://localhost/Home/comments/9/john

Where first parameter is id and second is name as defined in parameter which will be received in parameters $id, $name .

Demo

Try it »

Codeigniter Controller


Codeigniter Controller is simply class name which contains methods inside it.


All the controllers are kept inside the application/controllers folder.

Codeigniter Controller Syntax

Lets start with a simple controller named as “Helloworld”.

Note Class name must start with uppercase
example:

class Helloworld extends CI_Controller {

}

Not Like This :

class helloworld extends CI_Controller {
// this invalid class name.

}

Now visit your site as :

http://localhost/Helloworld

Or

If you have not removed the index.php from url

http://localhost/index.php/Helloworld

Note : Default function index() will be called

Which is :

http://localhost/Helloworld/index

Codeigniter Install


Codeigniter Install : Steps to install Codeigniter.


Step by Step Codeigniter Installation Process

1. Download Latest Version 3.0.0 : Download Zip
2. Unzip it at your server in folder “codeigniter”. for example : xampp/htaccess/codeigniter

3. Now go to – application/config/config.php Here add base url ie. Base path of your application directory.

For example :

$config['base_url'] = 'http://localhost/codeigniter';

4. Now to go the : application/config/database.php Add database settings here :

For Example :

$active_group = 'default';
$query_builder = TRUE;

$db['default'] = array(
	'dsn'	=> '',
	'hostname' => 'localhost',
	'username' => 'root',
	'password' => '',
	'database' => 'your_database_name',
	'dbdriver' => 'mysqli',
	'dbprefix' => '',
	'pconnect' => FALSE,
	'db_debug' => TRUE,
	'cache_on' => FALSE,
	'cachedir' => '',
	'char_set' => 'utf8',
	'dbcollat' => 'utf8_general_ci',
	'swap_pre' => '',
	'encrypt' => FALSE,
	'compress' => FALSE,
	'stricton' => FALSE,
	'failover' => array(),
	'save_queries' => TRUE
);


5 . (Optional) : Remove Index.php from url

Create a .htaccess file at the root of the application for example at : xampp/htaccess/codeigniter – add here .htaccess file

Add the following code in the .htaccess file :

DirectoryIndex index.php
RewriteEngine on
RewriteCond $1 !^(index\.php|images|css|js|robots\.txt|favicon\.ico)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./index.php?/$1 [L,QSA]

6. You are done : Now your application is ready : Hit the url : http://localhost/codeigniter you will see default welcome message

Demo»
Download Demo»

Codeigniter Overview


Codeigniter is a php framework which enables user for rapid application development. It enables developers to develop projects much faster. Codeigniter Provides rich set of libraries for common task. Codeigniter is easy to learn with 0 setup configuration. Codeigniter is free to use.


Lastest Version : 3.0.0

Codeigniter Features

  • Codeigniter is Free
  • Codeigniter is Fast
  • Codeigniter is Light Weight
  • Codeigniter Follows MVC Pattern
  • Codeigniter Generates user friendly URL’s
  • Codeigniter Provides Excellent User Guide

Codeigniter encryption library example


Codeigniter encryption library example : $this->load->library(‘encrypt’); is used for encryption and decryption in codeigniter.


1. first Add encryption key in config.php file.

$config['encryption_key'] = "YOUR ENC KEY";

2. Load Library : $this->load->library(‘encrypt’);

3. Now you can encode/Decode Your message as Below

Encode :

$this->encrypt->encode($msg)

or 
$key = "Your more secure key";
$this->encrypt->encode($msg,$key)

Decode:

$this->encrypt->decode($msg)

or 
$key = "Your more secure key";
$this->encrypt->decode($msg,$key)