Brenelz Web Design Solutions

Web programming, the brenelz way!

Brenelz's Web Development Tips


CodeIgniter PHP Framework

Ever since I have been developing and programming websites, I have tried to be as proficient and use the best practises that I can. I have got increasingly frustrated with inline SQL statements and spaghetti code. This prevents the idea of code reuse, which I have always strived for. What could be better than being able to reuse your code in different projects?

I have talked to a number of developers, and the suggestion I have got for these problems is to use Object-Orientated PHP, or use a PHP framework. These 2 things can be a bit hard for new programmers to understand, and frankly I don’t have them mastered either. I still don’t use PHP frameworks often, but I think I might have found one that could change that. The framework I am talking about is CodeIgniter.

CodeIgniter is a SMALL, and LIGHTWEIGHT framework unlike the majority of other PHP frameworks I have dabbled in. CakePHP and Symfony are huge frameworks that contain more functionality but have a big learning curve. Symfony even requires you to have access to a command line. This is a huge drawback as a number of web hosts don’t allow access to a command line for security reasons.

CodeIgniter has great documentation and screencasts that can really help you learn how to use it. It uses the MVC design pattern. MVC stands for Model, View, Controller and helps eliminate some of that spaghetti code and inline SQL statements I was talking about early. CodeIgniter is formed in such a way that it is extremely flexible and allows you to incorporate your own programming with ease. It allows expansion with libraries, plugins, and helpers.

Now to give you a taste for some code as to how easy it is to use CodeIgniter.

$data[‘title’] = ‘My CodeIgniter Tutorial’;
$data[‘intro’] = ‘Read my tutorial – or else’;
$this->load->view(‘mainpage’, $data)

How this works is that you are passing the $data variable to the view. CodeIgniter helps you by extracting the array keys into variables automatically. Now you can use $title and $intro in your view. You can see already how this will help to separate your business logic from your presentation markup.

Wait, static information is great and all but how do we access the database using CodeIgniter. CodeIgniter uses a pattern called Active Record. This makes handling databases a real brease! Below is an example.

$this->load->library(‘database’); // connect to the database
$data[‘query’] = $this->database->get(‘products’); // get all rows from the products table
$this->load->view(‘mainpage’, $data);

Now in the view you can do this:

while( $row = $query->result() ) :
<h1>$row->productName</h1>
<p>$row->productDescription</p>
endwhile;

If you wish to add a where clause to your query you can do something like this:

$this->database->where(‘productID’, 4);
$data[‘query’] = $this->database->get(‘products’);

CodeIgniter Blog Tutorial (20 mins)

If you want more examples, please visit the documentation. I would definitely suggest that you try this great framework out. Download CodeIgniter here, and let me know what you think!


8 Comments

  1. xwero
    Posted October 5, 2008 at 2:11 am | Permalink

    The database class isn’t linked to the CI registry with database but with db.

    The database is loaded using $this->load->database().

  2. Posted October 6, 2008 at 12:47 pm | Permalink

    CodeIgniter is great, but check out KohanaPHP (a fork of CI). It’s PHP5 only, and fills a few major gaps that are present in CI (no GET queries?). It’s also community-driven, so you can contribute and voice your recommendations on what should be added to the core. I’ve used CI, CakePHP and Zend Framework and since dabbling with Kohana, I’m excited to use it for my next PHP project. Interested in hearing your thoughts.

  3. Posted October 6, 2008 at 12:51 pm | Permalink

    CodeIgniter is very simple, and easy to learn. But have got a lot of core bugs. For example session is stored in cookie. If you want to build complex MVC application, you should use CakePHP 1.2.

  4. Posted October 6, 2008 at 1:25 pm | Permalink

    I know that CakePHP is a more fully functional PHP Framework but it seems so massive for some off the stuff I want to do. Has much more functionality but with it comes a steep learning curve.

    I like the simplicity of CodeIgniter, and will have to continue to continue feeling out different frameworks. Till now I have I have done mostly all custom programming but code-reuse is important to me.

  5. pydev
    Posted October 6, 2008 at 5:53 pm | Permalink

    I know you guys are all php devs, but seriously I would check out the django project. http://www.djangoproject.com, I have used many frameworks from ASP :(, zend, magento, rails, django and code ignitor. I can honestly say that Django by far is my favorite. Generic Views and a database api that puts all the above mentioned frameworks to shame. It’s in python which i am sure is a deterrent for you guys, but believe me I think you will love not only Python, but Django.

  6. Posted October 6, 2008 at 6:11 pm | Permalink

    @pydev - you make some good points here. I have used ASP.NET and Rails as well, but really haven’t gotten to try out Django a whole lot.

    I have heard good things about it, and I am familiar with the Python language. How tough is Django to setup on the server? It always seemed a bit difficult, but I really haven’t spent that much time on it.

    Maybe in the future I will blog about Django.

  7. Jano
    Posted October 7, 2008 at 3:25 am | Permalink

    @skinny: Next version of CodeIgniter (v 1.7) will store custom session data in a database table, check the SVN http://dev.ellislab.com/svn/CodeIgniter/trunk

    Copied from the 1.7 Change Log
    “Updated the Sessions class so that any custom data being saved gets stored to a database rather than the session cookie (assuming you are using a database to store session data), permitting much more data to be saved.”

    Additionaly, if you don’t want to store custom session data in cookies with the current version of CodeIgniter there are several user contributed libraries in this respect.

    @pydev: Though I’ve been a CodeIgniter user for more than a year, django is the framework where I get most of my inspiration from. It’s beautifully structured, well thought and designed, implements very good DRY practices… While I’m still a PHP developer, I’ve even purchased 3 books about django.

    In fact, there are some django concepts that are being ported to PHP http://codeigniter.com/forums/viewthread/87887/ http://www.beberlei.de/calypso/ (and JavaScript http://dojotoolkit.org/book/dojo-book-0-9/part-5-dojox/dojox-dtl), like template inheritance.

  8. OtengiM
    Posted October 8, 2008 at 8:01 am | Permalink

    I use Django I love it and Python too but Im starting to look at PHP and really PHP doesn’t need a web framework or template, PHP is already a framework and template just people have to follow best practices and patterns but PHP 5.3 it is looking pretty good, easy and even have closures and all the bells and whistles of OOP.

Post a Comment

Your email is never shared. Required fields are marked *

*
*

Copyright © 2008 Brenelz Web Solutions.

Winnipeg Web Development by Brenelz Web Design Solutions Logo Brenelz Web Solutions