Main Content

CodeIgniter - an excellent PHP framework with an easy start point

Archive - Originally posted on "The Horse's Mouth" - 2013-04-06 11:01:24 - Graham Ellis

Frameworks are such a valuable tool to help you write a web application - they provide the common structure that you need in most applications in addition to the web server itself, and into which you can write your own application specific code. But there's often a steep learning curve with a framework, and a bloat which means that you end up wit something that works, but works far less efficiently than code written without a framework.

Under PHP, I've done quite a bit of work over the last few months with the Zend Framework, but flavour is this month from a training viewpoint seems to be CodeIgniter by Ellis Labs. It's an easy download, unpack it to get it working and "Hello World" can be implemented very quickly with a template of your web page filled in by the controller in just a few file changes. I'm impressed!

  -bash-4.1$ diff -r vanilla/public_html/ public_html
  Only in public_html/application/controllers: mycontroller.php
  Only in public_html/application/views: pages
  Only in public_html/application/views: templates
  Only in public_html: .htaccess
  Only in public_html: index.html
  -bash-4.1$


As with any system that's documented online, the EllisLabs / manaual pages have - even at the beginning - lots of ifs, buts can caveats, so I thought I would include the final content of the six files I added (nothing changed) to make this work.

My first example produces:



Here are the file contents:

index.html - to provide a home page outside the CodeIgniter tests

  <html>
  <head><title>Consultations dot org dot uk</title></head>
  <body><h1>Currently a test site!</h1>
  In use by <a href=http://www.wellho.net>Well House Consultants</a> for our training courses
  </body>
  </html>


.htaccess - to reroute everything else to CodeIgniter

  RewriteEngine on
  
  # Route home page to "locked out" special front page
  
  RewriteRule ^index.htm index.html [L]
  RewriteRule ^$ index.html [L]
  
  # Send all running pages except index.php, robots.txt and the user guide to index.php
  
  RewriteCond !^(index\.php|images|robots\.txt|user_guide)
  RewriteRule ^(.*)$ /index.php/ [L]


application/controllers/mycontroller.php - to provide a first controller

  <?php
  
  class Mycontroller extends CI_Controller {
  
    public function view($page = 'home') {
    $data = array('title' => "showing CodeIgniter");
    $this->load->view('templates/header', $data);
  //    $this->load->view('pages/'.$page, $data);
    $this->load->view('pages/main', $data);
     $this->load->view('templates/footer', $data);
    }
  }


application/views/pages/main.php - to provide the view called up by that controller

  [[Content here]]

application/views/templates/header.php - to provide the header used by all views

  <html>
  <head>
    <title><?= $title ?> - Well House Tutorial</title>
  </head>
  <body>
    <h1>Teaching you to write excellent PHP - <?= $title ?></h1>
  <i>This is the header</i>
  <hr />


application/views/templates/footer.php - to provide the footer used by all views

  <hr /><i>This is the footer</i><br /><br />
  <?php date_default_timezone_set('Europe/London'); ?>
  <a href=http://www.wellho.net>Well House Consultants</a>
  - <strong>©<?= date("Y") ?></strong>
  </body>
  </html>


Frameworks become more and more important in PHP development and in my "best practise" module on all PHP courses I now talk about Router / Model / View / Controller, and I can cove off examples as required. At present, CodeIgniter is just a formal quick demo - but I'm happy to be kept talking about it after the course, and to include it in private courses.

Source code links for the files used above:

Controller - [here]
Main view - [here]
Header - [here]
Footer - [here]
.htaccess for routing changes - [here]
dummy front page - [here]