Friday 6 January 2017

CodeIgniter Examples

CodeIgniter First Example

A controller is set up to handle static pages. A controller is a class that simplifies the work in CodeIgniter.
In a CodeIgniter framework URL a basic pattern is followed.
In the following URL,
http://abc.com/book/novel/
Here, 'book' is the controller class or you can say this is the controller name.
'novel' is the method that is called. It extends to CI_Controller to inherit the controller properties.

An Example to print Hello World

Create file in Controllers
In Controller create a file named "Hello.php". This file will be saved in the Controller folder of your CodeIgniter. Write the following coding.
  1. <?php  
  2. defined('BASEPATH') OR exit('No direct script access allowed');  
  3.   
  4. class Hello extends CI_Controller {  
  5.       
  6.     public function index()  
  7.     {  
  8.         $this->load->view('hello_world');  
  9.     }  
  10. }  
  11. ?>  
Here, your controller class is Hello, extends to CI_Controller means they can access the methods and variables defined in the controller class.
$this loads views, libraries and command the framework.
Create file in Views
Create a file named "hello_world.php". Save this file in the View folder of your CodeIgniter. Write the following coding.
  1. <!DOCTYPE html>  
  2. <html>  
  3. <head>  
  4.     <title>Hello World</title>  
  5. </head>  
  6. <body>  
  7.   
  8.  <p>Hello World!!</p>  
  9.  </body>  
  10. </html>  
Run the Controller file
To run the file, follow the path http://localhost/CodeIgniter/index.php/Hello/

CodeIgniter URL

CodeIgniter URLs are SEO friendly. Instead of using a 'query-string' approach, it uses a segment-based approach.

Basic URL structure

abc.com/class/function/ID
class represents controller class that needs to be invoked.
function is the method that is called.
ID is any additional segment that is passed to controllers.

What is site_url();

You can pass a string or an array in a site_url() function. In this example we'll pass a string,
echo site_url('book/novel/fiction');
The above function will return something like this
http://abc.com/index.php/book/novel/fiction
In this example we'll pass an array,
$data = array('book', 'novel', 'fiction');
echo site_url($data);

What is base_url();

It returns your site base URL, if mentioned any, in the config file. On passing base_url(), it also returns the same thing as site_url() with eliminating index.php. This is useful because here you can also pass images or text files. Here also you can pass a string or an array.
In this example we'll pass a string,
echo base_url("book/novel/fiction");
The above function will return something like this http://abc.com/ book/novel/fiction

What is uri_string();

It returns the URI segment of a page. For example, if your URL is,
http://abc.com/book/novel/fiction
Then, uri_string() will return
Book/novel/fiction

What is current_url();

Calling this function means, it will return the full URL of the page currently viewed.
Please note -> calling this function is same as calling uri_string() in site_url().
current_url() = site_url(uri_string());

What is index_page();

It will return your site's index_page which you have mentioned in your config file. By default, it is always index.php file.
You can change it with the help of .htaccess file.

anchor()

It creates a standard HTML link based on your local site URL. For example,
Echo anchor('book/novel/fiction', 'My Collection, 'title="book name");
It will give the following result,

anchor_popup()

It is identical to anchor() but it opens the URL in a new window.

mailto()

It creates a HTML email link. For example,
Echo mailto('abc@abc_site.com', 'To contact me click here')

url_title()

It takes a string as an input and creates a human friendly environment. For example,
  1.    
  2. $title = "CodeIgniter's examples"  
  3. $url_title() = url_title($title);  
Output will be "CodeIgniters-examples"
If you'll pass a second parameter, it defines word delimiter.
  1.    
  2. $title = "CodeIgniter's examples"  
  3. $url_title() = url_title($title'_');  
Output will be "CodeIgniters_examples"
If you'll pass a third parameter, it defines uppercase and lowercase. You have Boolean options for this, TRUE/FALSE.
  1.    
  2. $title = "CodeIgniter's examples"  
  3. $url_title() = url_title($title'_', TRUE);  
Output will be "codeigniters_examples"

Basic Site creation in CodeIgniter

Here, we'll learn how to create a basic site with the help of CodeIgniter.
In controllers folder we'll create a file named as Form.php
  1. <?php  
  2. defined('BASEPATH') OR exit('No direct script access allowed');  
  3.   
  4. class Form extends CI_Controller {  
  5.   
  6.     public function index()  
  7.     {  
  8.         $this->load->view('header');  
  9.         $this->load->view('nav');  
  10.         $this->load->view('content');  
  11.         $this->load->view('footer');  
  12.     }  
  13. }     
  14. ?>  
We have created different files for header, nav, content and footer and all these files are loaded in the contoller's file.
File header.php in application/views
  1. <!DOCTYPE html>  
  2. <html>  
  3. <head>  
  4.     <title>Basic Site</title>  
  5.     <style type="text/css">  
  6.           
  7.         body, html{margin:0; padding:0;}  
  8.   
  9.         body{  
  10.             background-color:#eee;  
  11.         }  
  12.         h1, h2, h3, h4, p, a, li, ul{  
  13.             font-family: arial, sans-serif;  
  14.             color: black;  
  15.             text-decoration: none;  
  16.         }  
  17.         #nav{  
  18.             margin: 50px auto 0 auto;  
  19.             width: 10000px;  
  20.             background-color: #888;  
  21.             height: 15px;  
  22.             padding: 20px;  
  23.         }  
  24.   
  25.         #nav a:hover{  
  26.             color: red;  
  27.   
  28.         }  
  29.         #nav ul{  
  30.             list-style: none;  
  31.             float: left;  
  32.             margin: 0 50px;  
  33.         }  
  34.   
  35.         #nav ul li{  
  36.             display: inline;  
  37.         }  
  38.   
  39.         #content{  
  40.             width: 1000px;  
  41.             min-height: 100%;  
  42.             margin: 0 auto;  
  43.             padding: 20px;  
  44.         }  
  45.   
  46.         #footer{  
  47.             width: 400px;  
  48.             height: 15px;  
  49.             margin: 0 auto;  
  50.             padding: 20px;  
  51.         }  
  52.   
  53.         #footer p{  
  54.             color: #777;  
  55.         }  
  56.     </style>  
  57. </head>  
File nav.php in application/views
  1. <body>  
  2. <div id="container">  
  3.     <div id="nav">  
  4.         <ul>  
  5.             <li><a href="#">Home</a></li>  
  6.             <li><a href="#">About Us</a></li>  
  7.             <li><a href="#">Contact Us</a></li>  
  8.             <li><a href="#">Career</a></li>  
  9.         </ul>  
  10.     </div>  
File content.php in application/views
  1. <div id="content">  
  2.         <h1>Welcome to my site</h1>  
  3.         <p>CodeIgniter is PHP driven framework but it's not a PHP substitute.   
  4.         Diving into CodeIgniter doesn?t mean you are leaving PHP behind.   
  5.         PHP is a server-side scripting language for building dynamic web-based applications.</p>  
  6. </div>  
File footer.php in application/views
  1. <div id="footer">  
  2.         <p>Copyright (c) 2016 ABCWorld.com All Rights Reserved</p>  
  3.     </div>  
  4.   
  5. </div>  
  6. </body>  
  7. </html>  
Final output is shown below with URL localhost/site_example/index.php/Form.
Codeigniter Basic site creation in codeignter 7

CodeIgniter Methods

In the earlier Hello World example, our method name is index(). By default Controller always calls index method. If you want a different method, then write it in the Controller's file and specify its name while calling the function.
Codeigniter Methods 1
Look at the URL, there is no method name is mentioned. Hence, by default index method is loaded.

Method other than index()

Here, we have mentioned a method called newFunction(). Now we have to call this new method to run our program.
Create a controller page Hello.php in application/controllers.
  1. <?php  
  2. defined('BASEPATH') OR exit('No direct script access allowed');  
  3.   
  4. class Hello extends CI_Controller {  
  5.   
  6.     public function newfunction()  
  7.     {  
  8.         $this->load->view('hello_world');  
  9.     }  
  10. }  
  11. ?>  
Look at the above snapshot, we have created a function newFunction.
Create a view page hello_world.php in application/views.
  1. <!DOCTYPE html>  
  2. <html>  
  3. <head>  
  4.     <title>Hello World Example</title>  
  5. </head>  
  6. <body>  
  7.     <p>Hello World!!</p>  
  8. </body>  
  9. </html>  
To run this program on our browser, follow path
http://localhost/CodeIgniter/index.php/Hello/newFunction
Codeigniter Methods 4
Look at the above snapshot, we created the Controller's function as newFunction and specified it in the URL after Controller's name.
Here, /index.php/Hello is Controller's name.
And /newFunction is the Function name.

Remapping Method Calls

Second segment of URI determines which method is being called. If you want to override it you can use _remap() method.
If you have mentioned _remap() method in your controllers, it will always get called even if URI is different. It overrides the URI.
  1. public function _remap($methodName)  
  2. {  
  3.             if ($methodName === 'a_method')  
  4.             {  
  5.             $this->method();  
  6.             }  
  7.             else  
  8.             {  
  9.             $this->defaultMethod();  
  10.             }  
  11. }  

CodeIgniter Helper


What is Helper

In CodeIgniter there are helpers which are there to help you with different tasks. Every helper file is a collection of functions aiming towards a particular role. Some of the helpers are 'file helpers' which help you to deal with the file, 'text helpers' to perform various text formatting routines, 'form helpers' to create form element, 'cookie helpers' set and read cookies, 'URL helpers' which assist in creating links, etc.
Helpers are not written in Object Oriented format, instead they are simple, procedural functions, independent of each other.
To use helper files, you need to load it. Once loaded it is globally available to your controller and views. They are located at two places in CodeIgniter. CodeIgniter will look first for a helper in application/helpers folder and if not found there then it will go to system/helpers folder.

Loading a Helper

A helper can be loaded in controller constructor which makes them globally available, or they can also be loaded in specific functions which need them.
It can be loaded with the following code:
  1. $this->load->helper('file_name');  
Write your file name here at the place of file_name.
To load URL helper,
  1. $this->load->helper('url');  
You can also auto-load a helper if your application needs that helper globally by adding the helper in application/config/autoload.php file.

Loading Multiple Helpers

To load multiple helpers, specify them in an array,
  1. $this->load->helper(  
  2.         array('helper1', 'helper2', 'helper3')  
  3. );  

html Helper Example

We are going to show you an example of html helper by using it in a basic website page. Here, we'll auto-load our helper.
Go to autoload.php file via application/config/autoload.php
  1. $autoload['helper'] = array('html');  
In the above file, name your helper, here it is html.
In application/controllers there is file Form.php
  1. <?php  
  2. defined('BASEPATH') OR exit('No direct script access allowed');  
  3.   
  4. class Form extends CI_Controller {  
  5.   
  6.     public function index()  
  7.     {  
  8.         $this->load->view('header');  
  9.         $this->load->view('nav');  
  10.         $this->load->view('content');  
  11.         $this->load->view('footer');  
  12.     }  
  13. }     
  14. ?>  
In application/views there is file header.php
The first line is coded in php tag.
  1. <?php echo doctype("html5"); ?>  
  2. <html>  
  3. <head>  
  4.     <title>Basic Site</title>  
  5.     <style type="text/css">  
  6.           
  7.         body, html{margin:0; padding:0;}  
  8.   
  9.         body{  
  10.             background-color:#eee;  
  11.         }  
  12.         h1, h2, h3, h4, p, a, li, ul{  
  13.             font-family: arial, sans-serif;  
  14.             color: black;  
  15.             text-decoration: none;  
  16.         }  
  17.         #nav{  
  18.             margin: 50px auto 0 auto;  
  19.             width: 10000px;  
  20.             background-color: #888;  
  21.             height: 15px;  
  22.             padding: 20px;  
  23.         }  
  24.   
  25.         #nav a:hover{  
  26.             color: red;  
  27.   
  28.         }  
  29.         #nav ul{  
  30.             list-style: none;  
  31.             float: left;  
  32.             margin: 0 50px;  
  33.         }  
  34.   
  35.         #nav ul li{  
  36.             display: inline;  
  37.         }  
  38.   
  39.         #content{  
  40.             width: 1000px;  
  41.             min-height: 100%;  
  42.             margin: 0 auto;  
  43.             padding: 20px;  
  44.         }  
  45.   
  46.         #footer{  
  47.             width: 400px;  
  48.             height: 15px;  
  49.             margin: 0 auto;  
  50.             padding: 20px;  
  51.         }  
  52.   
  53.         #footer p{  
  54.             color: #777;  
  55.         }  
  56.     </style>  
  57.   
  58.   
  59.   
  60. </head>  
The other half coding for file header.php is shown in below snapshot.
In application/views there is file content.php
Here also the heading is written in php tag instead of html.
  1. <div id="content">  
  2.         <?php echo heading("Welcome to my site", 1); ?>  
  3.         <p>In a professional context it often happens that private or corporate clients   
  4.         corder a publication to be made and presented with the actual content still not being ready.  
  5.          Think of a news blog that's filled with content hourly on the day of going live. However,   
  6.          reviewers tend to be distracted by comprehensible content, say, a random text copied from   
  7.          a newspaper or the internet. The are likely to focus on the text, disregarding the layout  
  8.           and its elements. Besides, random text risks to be unintendedly humorous or offensive,  
  9.            an unacceptable risk in corporate environments.</p>  
  10.     </div>  
Final output is just like a normal page as shown below with the URL localhost/helper/index.php/Form.
COdeigniter Helper 6But when we will see its open source (by pressing ctrl+u), you will see the following coding which simply shows html code and not php code what we have written above.
COdeigniter Helper 7

CodeIgniter Library


What is a Library

CodeIgniter provide a rich set of libraries. It is an essential part of CodeIgniter as it increases the developing speed of an application. It is located in the system/library.

Loading Library

CodeIgniter library can be loaded as following,
  1. $this->load->library('class_name');  
Here, class name should be replaced by the name of the library.
To load multiple libraries, use following code,
  1. $this->load->library(array('email', 'table'));  

Creating Libraries

All the CodeIgniter libraries are placed in system folder. But in case if you want to use any other library in your application you can create it. There is no limitation for libraries. But your created libraries will be stored in the application/libraries folder. This is done to separate your local and global framework resources.
There are three methods to create a library,
  • Creating an entire new library
  • Extending native libraries
  • Replacing native libraries

Creating an entire new library

It should be placed in application/libraries folder.
Naming Conventions
  • File name first letter has to be in uppercase letter, like Mylib.php
  • Class name first letter should also be in uppercase letter
  • File name and class name should be same.
Basic syntax:
Suppose your file name is Mylib.php, then syntax will be as follows,
Loading Mylib.php
It can be loaded with the following line,
  1. $this->load->library('mylib.php')  
Note: You can write library name in any one of the upper or lower case letter.
Accessing mylib.php
Once it is loaded, you can access your class using the lower case letter because object instances are always in lower case.
  1. $this->mylib->some_method();  

Extending Native Libraries

You can also add some extended functionality to a native library by adding one or two methods. It will replace the entire library with your version. So it is better to extend the class. Extending and replacing is almost identical with only following exceptions.
  • Class declaration must extend the parent class.
  • New class name and filename must be prefixed with MY_.
For example, to extend it to native Calendar, create a file MY_Calendar.php in application/libraries folder. Your class will be declared as,class MY_Calendar extends CI_Calendar}

Replacing Native Libraries

Naming the new file and class name same as the native one will cause the CodeIgniter new one instead of native one. File and class declaration should be exactly same as the native library.
For example, to replace native Calendar library, you'll create a file Calendar.php in application/libraries. And your class will be,
  1. Class CI_Calendar {  
  2. }  


URL Routing

URLs in CodeIgniter are designed to be short and search engine friendly. It should make more sense to the visitors. A user should get an idea about the page content through its URL.
For example, http://abc.com/codeigniter/routing_url
The above URL example makes more sense and gives a brief idea to the users what it is about.
One should always go for the SEO friendly URL.
URL routing is a technique through which it converts SEO friendly URLs into a server code format that understands it easily and drives a request to corresponding handler scripts.

Setting your own Routing Rules

Routing rules are defined in routes.php file at the location application/config. In this file you'll see $route array, it permits you to specify your own routing criteria. Routes can be classified in two ways, either using Wildcards or Regular Expressions.

Wildcards

There are two types of wildcards:
  • :num−series containing only numbers will be matched.
  • :any−series containing only characters will be matched.
Using :num
  1. $route['(blog/:num)'] = 'women/social/$1';  
URL containing first segment as 'blog' and second segment as any 'number' will represent to the URL containing 'women' class and 'social' method passing in the match as the variable to the function.
It means when we'll pass URL http://www.abc.com/blog/1
Note: Here, you can pass any number instead of 1 in the URL.
It will be directed to http://www.abc.com/women/social
Using :any
  1. $route['(blog/:any)'] = 'women/social';  
URL containing first segment as 'blog' and second segment as anything will represent to the URL containing 'women' class and 'social' method.
It means when we'll pass URL http://www.abc.com/blog/xyz
Note: Here, you can pass anything in the last segment of URL.
It will be directed to http://www.abc.com/women/social

Regular Expression

Regular expressions are also used to redirect routes.
  1. $route['blog'(a-zA-Z0-9]+)'] = 'women/social';  
You can create your own regular expression to run your URL.

URL Suffix

To add a suffix in your URL, go to file config.php in application/config folder and add suffix you want as shown below. We have added .jsp as the suffix.
  1. $config['url_suffix'] = '.jsp';  
For example, if our URL ishttp://www.abc.com/women/social
Then after adding suffix, our URL will becomehttp://www.abc.com/women/social.jsp


CodeIgniter Hooks

In CodeIgniter, hooks are events which can be called before and after the execution of a program. It allows executing a script with specific path in the CodeIgniter execution process without modifying the core files. For example, it can be used where you need to check whether a user is logged in or not before the execution of controller. Using hook will save your time in writing code multiple times.
There are two hook files in CodeIgniter. One is application/config/hooks.php folder and other is application /hooks folder.
In other language, if you want to run a code every time after controller constructor is loaded, you can specify that script path in hooks.

Enabling Hooks

To enable Hook, go to application/config/config.php file and set it TRUE as shown below.
  1. $config['enable_hooks'] = TRUE;  

Defining a Hook

A hook can be defined in the application/config/hooks.php file. Each hook is defined as an array consisting of the following terms.
  1. $hook['pre_controller'] = array(  
  2.             'class' => 'Classname',  
  3.             'function' => 'functionname',  
  4.             'filename' => 'filename.php',  
  5.             'filepath' => 'hooks',  
  6.             'params' => array('element1''element2''element3')  
  7.             );  
class - Here, you have to mention the name of your class defined in the hooks.php file. If you are using procedural function instead of a class, leave it blank.
function - Mention the function name you are calling.
filename - The file name created in application/hooks folder and containing class and function name mentioned above.
filepath - Here you have to mention the name of the directory which contains your script. Your script must be located inside the application folder. If your script is located in application/hooks folder, then your path will be simply hooks. But if your script is located in application/hooks/office folder, then your path will be hooks/office.
params - It includes the parameters which you want to pass in your script and it's optional.

Multiple calls to the same Hook

You can use array multi-dimensional to use the same hook point with more than one script.
  1. $hook['pre_controller'][] = array(  
  2.                     'class' => 'Classname1',  
  3.                     'function' => 'functionname1',  
  4.                     'filename' => 'filename1.php',  
  5.                     'filepath' => 'hooks',  
  6.                     'params' => array('element1''element2''element3')  
  7.                     );  
  8.   
  9.  $hook['pre_controller'][] = array(  
  10.                     'class' => 'Classname2',  
  11.                     'function' => 'functionname2',  
  12.                     'filename' => 'filename2.php',  
  13.                     'filepath' => 'hooks',  
  14.                     'params' => array('element4''element5''element6')  
  15.                     );  
Bracket [] enables you to have same hook point with multiple scripts. Your execution order will be same as the array defined.

Hook Points

The list of hook points is shown below.
  • pre_system
  • It is called much before the system execution. Only benchmark and hook class have been loaded at this point.
  • pre_controller
  • It is called immediately prior to your controller being called. At this point all the classes, security checks and routing have been done.
  • post_controller_constructo
  • It is called immediately after your controller is started, but before any method call.
  • post_controller
  • It is called immediately after your controller is completely executed.
  • display_override
  • It is used to send the final page at the end of file execution.
  • cache_override
  • It enables you to call your own function in the output class.
  • post_system
  • It is called after the final page is sent to the browser at the end of the system execution.

Hook Example

1) First of all enable the hook in your CodeIgniter folder as mentioned above.
2) Create a Controller file example.php in application/controller folder
  1. <?php  
  2. defined('BASEPATH') OR exit('No direct script access allowed');  
  3. class Example extends CI_Controller {  
  4. public function index()  
  5.     {  
  6.         echo "CodeIgniter Tutorial";  
  7. }  
  8. }  
On running the above program with URL,
http://localhost/hooks/index.php/example, following output will appear.
Codeigniter Hooks 1
3) Create a hooks file exm.php in application/hooks folder.
  1. <?php  
  2. defined('BASEPATH') OR exit('No direct script access allowed');  
  3. class Exm extends CI_Controller {  
  4. public function tut()  
  5.     {  
  6.         echo "Welcome to suresh. This is ";  
  7.     }  
  8. }  
  9. ?>  
4) Now you have to define your hook in the application/config/hooks folder.
  1. <?php  
  2. defined('BASEPATH') OR exit('No direct script access allowed');  
  3.   
  4. $hook['pre_controller'] = array(  
  5.         'class' => 'Exm',  
  6.         'function' => 'tut',  
  7.         'filename' => 'exm.php',  
  8.         'filepath' => 'hooks',  
  9.         );  
  10. ?>  
5) Now again run your program with the same URL and see the result.
Codeigniter Hooks 2

Passing Parameters in CodeIgniter

Now we'll see an example to pass parameters from controller to view.
1) Download CodeIgniter and name it. We have named it as params.
2) Create a file para.php in application/controllers folder.
  1. <?php  
  2. if(!defined('BASEPATH')) exit('No direct script access allowed');   
  3. class Para extends CI_Controller{  
  4. // declaring variables  
  5. var $name;  
  6.     function __construct(){  
  7.     parent::__construct();  
  8. // passing value  
  9. $this->name="CodeIgniter";  
  10. }  
  11. function tut()  
  12. {  
  13. $data['name']=$this->name;   
  14. // define variable sent to views  
  15. $this->load->view('para_view',$data);  
  16.  }  
  17. }  
  18. ?>  
3) Create a file para_view.php in application/views folder.
  1. <!DOCTYPE html>  
  2. <html>  
  3. <head>  
  4.     <title>Passing Parameter</title>  
  5. </head>  
  6. <body>  
  7.     <h3>Hello, This is <?php echo $name ; ?> Tutorial.</h3>  
  8. </body>  
  9. </html>  
4) Run the program on your browser with URL
http://localhost/params/index.php/para/tut
5) Following output will appear on the screen.
Codeigniter Passing parameters in codeigniter 1

CodeIgniter Driver

Drivers are introduced in CodeIgniter 2.0 and onwards.

What are Drivers

These are special type of library that has a parent class and many child classes. These child classes have access to the parent class, but not to their siblings. It enables you to make more elegant classes and more elegant syntax inside your controller.
Drivers are found in system/libraries folder in CodeIgniter folder.

Initializing Driver

To initialize a driver, write the following syntax.
  1. $this->load->driver('class_name');  
Here, class_name is the driver name you want to invoke.
For example, to invoke a driver class main_class, do the following.
  1. $this->load->driver('main_class');  
To invoke its method,
  1. $this->main_class->a_method();  
And then child classes can be called directly through the parent class, without initializing them.
  1. $this->main_class->first_child->a_method();  
  2.   
  3.     $this->main_class->second_child->a_method();  

Creating Own Drivers

There are three steps to create a driver in CodeIgniter.
  1. Making file structure
  2. Making driver list
  3. Making driver(s)

Making file structure

Go to system/libraries folder of CodeIgniter and make a new folder My_driver. Inside this folder make a file My_driver.php.
Now make a new folder inside My_driver folder, name it as drivers. Inside this new folder make a file, My_driver_first_driver.php.
The following file structure will be shown.
  1. /libraries  
  2.         /My_driver  
  3.             My_driver.php  
  4.             /drivers  
  5. My_driver _first_driver.php  
Codeigniter Driver 1
In CodeIgniter, driver library structure is such that subclasses don?t extend and hence they don't inherit the properties or methods of the main driver (in this case it is My_driver).
  • My_driver - it is a class.
  • My_driver.php - Parent driver
  • My_driver_first_driver.php - Child driver

Making Driver List

In the file My_driver.php in system/libraries/My_driver folder write the following code,
  1. <?php  
  2. defined('BASEPATH') OR exit('No direct script access allowed');  
  3. class CI_My_driver extends CI_Driver_Library  
  4. {  
  5.     function __construct()  
  6.     {  
  7.         $this->valid_drivers = array('first_driver');  
  8.     }  
  9.     function index()  
  10.     {  
  11.         echo "<h1>This is Parent Driver</h1>";  
  12.     }  
  13.      }  
In the file My_driver_first_driver.php in system/libraries/My_driver/drivers write the following code,
  1. <?php  
  2. defined('BASEPATH') OR exit('No direct script access allowed');  
  3. class My_driver_first_driver extends CI_Driver  
  4. {  
  5.     function first()  
  6.     {  
  7.         echo "<h1>This is first Child Driver</h1>";  
  8.     }  
  9.      }  
  10. ?>  
Make a controller file Mydrive.php in application/controllers with the following code,
  1. <?php  
  2. defined('BASEPATH') OR exit('No direct script access allowed');  
  3.     class Mydrive extends CI_Controller  
  4.  {  
  5.     function __construct()  
  6.     {  
  7.              parent::__construct();  
  8.          $this->load->driver('my_driver');  
  9.       }  
  10.     public function invoke1()  
  11.     {  
  12.        $this->my_driver->index();  
  13. }  
  14.     public function invoke2()  
  15.     {       
  16.        $this->my_driver->first_driver->first();  
  17.     }  
  18. }?>  
  19.    
On your browser run the URL, http://localhost/driver/index.php/mydrive/invoke1
Codeigniter Driver 2
Look at the above snapshot, the parent driver class in invoked with the function invoke1.
Now run the URL, http://localhost/driver/index.php/mydrive/invoke2
Codeigniter Driver 3
Look at the above snapshot, the child driver class in invoked with the function invoke2.






2 comments: