PHP
  Home arrow PHP arrow Page 4 - Introduction to the CodeIgniter PHP Framework
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
Google.com  
PHP

Introduction to the CodeIgniter PHP Framework
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 29
    2008-08-13


    Table of Contents:
  • Introduction to the CodeIgniter PHP Framework
  • Start using CodeIgniter
  • Activating support for MySQL databases
  • Developing the first application with Code Igniter

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    Introduction to the CodeIgniter PHP Framework - Developing the first application with Code Igniter
    ( Page 4 of 4 )

    As I mentioned in the introduction, Code Igniter uses the Model-View-Controller pattern to build web applications. This comes in handy for separating business and application logic from the visual presentation. Thus, since I plan to develop a simple application that will display a simple message on the screen, the first thing that I’m going to do is define the corresponding controller class.

    To do this, I’ll create a class called “HelloWorld,” which will be saved to the /system/application/controllers/ folder as “helloworld.php." The signature of this sample class looks like this:


    <?php

    class HelloWorld extends Controller{

    function HelloWorld(){

    // load controller parent

    parent::Controller();

    }

    function index(){

    $data['title']='My first application created with Code Igniter';

    $data['message']='Hello world!';

    // load 'helloworld' view

    $this->load->view('helloworld',$data);

    }

    }

    ?>


    Now that you know how the “HelloWorld” controller class looks, it’s necessary to dissect it in different parts and explain the functionality. In the first place, you should notice that each time a new controller is created, it must extend the default controller provided by Code Igniter. Therefore, this sample class is defined as a subclass of this controller.

    Then, this sample class loads the parent’s constructor and then defines a brand new method called “index().” This method has a special meaning for Code Igniter, since it will be called automatically whenever it finds it was implemented by a specific controller.

    Still with me? Great! Then, it’s time to analyze what this method actually does. As you can see, it defines the data that will be passed in to the view file to be displayed later on. This data is first stored on a simple $data array, and second transferred to a view template, called “helloworld.php.”

    Each time a view file needs to be loaded, Code Igniter uses a “Loader” class, which is included by default by each new controller created. So, in this case the below expression:


    $this->load->view('helloworld',$data);


    simply loads the corresponding view file and passes the data that will be printed on the browser to it. Pretty intuitive, right?

    Now that you've hopefully grasped how the previous “HelloWorld” controller class works, I’m sure that you’ll want to see how the respective “helloworld.php” view file looks. Below I included the definition of the file in question, which should be saved to the /system/application/views/ folder:


    <html>

    <head>

    <title><?php echo $title;?></title>

    </head>

    <body>

    <h1><?php echo $message?></h1>

    </body>

    </html>


    As shown above, the structure of the “helloworld.php” view file is extremely simple. It only contains a couple of “echo” PHP statements, which are used to display the data passed by the controller. Also, you should recall that this data was stored on a $data array, so its respective keys are automatically turned into variables within the view template.

    So far, so good. At this point, you hopefully understand how the controller passes certain data in the form of an array to a view file to be echoed to the browser. However, it’s possible that you’re wondering how to test the previous “HelloWorld” controller with Code Igniter.

    Code Igniter uses a special routing process that permits you to invoke a specific controller by using an URL. The first URL segment is the name of the controller that will be loaded by a certain application, then the second segment corresponds to the name of a method of the controller that will be invoked. The third segment is an optional argument taken by this method.

    This explanation might sound pretty confusing to you. However, the following code sample should dissipate any possible doubts you might have. To call the previous “HelloWord” controller, I’d type the following URL:


    http://localhost/codeigniter/index.php/helloworld/


    As you can see, the first URL segment calls the “HelloWorld” controller, and automatically executes its “index()” method, which displays the view file previously created. Try it for yourself and you’ll see the classic “Hello World” message printed on your screen.

    In addition, to clarify how Code Igniter’s routing process works, say that I changed the definition of the “HelloWorld” controller class and now it looks like this:


    class HelloWorld extends Controller{

    function HelloWorld(){

    // load controller parent

    parent::Controller();

    }

    function display(){

    $data['title']='My first application created with Code Igniter';

    $data['message']='Hello world!';

    // load 'helloworld' view

    $this->load->view('helloworld',$data);

    }

    }


    In this case, the URL that should be used to call the “display()” method of the controller would be:


    http://localhost/codeigniter/index.php/helloworld/display/


    Pretty easy to grasp, right? By using this routing method, Code Igniter assures that all of the URLs generated by a PHP application will look clear and be search engine-friendly as well.

    And with this final hands-on example, I’m finishing this first tutorial of this series. As you saw before, I provided you with concrete material that hopefully will help you start mastering the basic features of Code Igniter. You have a lot of fun ahead!

    Final thoughts

    In this first chapter of the series, I walked you through the core concepts that surround the use of the Code Igniter PHP framework. You hopefully learned how to correctly setup its configuration files, and how to create a simple web application comprised of a single controller and a view file.

    I’m only scratching the surface when it comes to discussing the vast set of handy features that come bundled with Code Igniter. Therefore, in the next article, I’ll be covering the use of models and the utilization of some of its core classes.

    Don’t miss the upcoming tutorial!



     
     
    >>> More PHP Articles          >>> More By Alejandro Gervasio
     

       

    PHP ARTICLES

    - Implementing Factory Methods in PHP 5
    - Merging a File Split for FTP Upload using PHP
    - Getting Data from Yahoo Site Explorer Inboun...
    - Method Chaining: Adding More Selecting Metho...
    - How to Split a File During an FTP Upload Usi...
    - Expanding a Custom CodeIgniter Library with ...
    - Using the Yahoo Site Explorer Inbound Links ...
    - Building a CodeIgniter Custom Library with M...
    - Building an E-mini Trading System Using PHP ...
    - Completing the MySQL Class with Method Chain...
    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...
    - Mastering WHILE Loops for PHP and MySQL





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 3 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek