Welcome Guest, Not a member yet? Register   Sign In
My Codeigniter Observations
#1

[eluser]James Spibey[/eluser]
I'm almost done on my first codeigniter application and I'd just like to write a bit about my experiences with it and how I've used it. The application in question gets around 500,000 views per month, has taken nearly 6 months (part-time) to complete and has over 500 application source files (not including CI).

Firstly, this is a great, great product. The key for me with CI is that it never gets in the way of my coding. I spent almost zero time getting the framework running and since then there have been very few situations where I have had to dig into the CI code to figure out why something wasn't working the way I wanted. The great thing about CI is the wealth of libraries and helpers that you get with it for free - anything I could think of which I'd need during development was there, ready and waiting and working perfectly. And if it wasn't there, I just found an open source library which did do it and just dropped it in. Awesome.

So, if there are any tips I do have, they are focused more on application structure and MVC design.

Get your models, views and controllers straight in your mind.

For a while I couldn't understand how to structure my application because I'd not used MVC before. However, once I'd got used to it, it was pretty easy. In my opinion, your model is just a gateway to your database, it marshalls data back and forth between your database and your controller - don't try and make it do any more than this.

Your views are just that, views. Don't put any application logic in here at all. If you need to do anything in your view which is starting to look like php, check that this couldn't be better achieved by using a helper.

Your controllers are the daddy of the application. They receive input from the client, sanitize and validate it, and then pass it onto the model. The data they receive back from the model should be wrapped as php objects which then get passed onto the view (see next point).

Make your Model return a collection of typed object rather than arrays.

The most obvious way to get data in your model is to just select from the database and then return an array of results (with each result being an array of fields or an object). Here's a simple example which gets a list of customers from the database
Code:
class Customermodel extends Model {
{
  function get_customers()
  {
    $query = $this->db->getwhere('customers');
    return $query->result();
  }
}
Instead, have your model package each record returned from the database into an object (in this case a 'Customer' object).
Code:
class Customermodel extends Model {
{
  function get_customers()
  {
    $query = $this->db->getwhere('customers');
    return $this->format_customers($query);
  }

  function format_customers($query)
  {
    $customers = $query->result_array();
  
    $new_customers = array();

    foreach($customers as $customer)
    {
      $new_customers[] = new Customer($customer);
    }

    return $new_customers;
  }
}
And then have your Customer class (in your Libraries folder) look like this
Code:
class Customer {
  
  function Customer($customer = null)
  {
    if(isset($customer))
    {
      $this->_load($customer);
    }
  }
  
  function _load($customer)
  {
    while (list ($key, $val) = each ($customer))
    {
      $this->$key = stripslashes($val);
    }
  }

  // Example function
  function get_full_name()
  {
    return $this->fore_name + ' ' + $this->last_name;
  }

Now, this might seem a little long-winded but it means that you can wrap the data from the database into an object and then have specific logic attached to the data to control how it is displayed etc in a consistent way. So, if I wanted to change the way a customer's name is displayed throughout the application, I only need to change the get_full_name() function and it is instantly changed everywhere (a simple example but you get the idea). When you are dealing with lots of complicated data this will simplify the code in your views and controllers no end and will make your code more robust.

Other Bits
- Have all your controllers inherit from a common base class (called something like BaseController) which in turn inherits from Controller. This makes it much easier to share code between controllers and pages (such as common login/logout and page access code).

- If you are creating a website with user authentication, encapsulate the current logged in user in a class (like the Customer one above) and store that in session (database or PHP sessions). Then have this loaded automatically in your basecontroller so that every controller has access to it as soon as they are loaded. This will let you manage access to pages and identify what privileges the currently logged in user has much more simply and consistently.

There's tonnes more things I've found along the way but this is far too long already. This is just the way I've done it and you have found a different route but I've found that I've barely had to change my architecture at all throughout the project to accommodate the client using the design above so that makes it perfect for me!


Messages In This Thread
My Codeigniter Observations - by El Forum - 01-21-2008, 05:30 AM
My Codeigniter Observations - by El Forum - 01-21-2008, 06:15 AM
My Codeigniter Observations - by El Forum - 01-21-2008, 06:46 AM
My Codeigniter Observations - by El Forum - 01-21-2008, 04:36 PM
My Codeigniter Observations - by El Forum - 01-22-2008, 04:04 AM
My Codeigniter Observations - by El Forum - 01-22-2008, 06:24 AM
My Codeigniter Observations - by El Forum - 01-22-2008, 07:29 AM
My Codeigniter Observations - by El Forum - 01-22-2008, 08:17 AM
My Codeigniter Observations - by El Forum - 01-22-2008, 09:04 AM
My Codeigniter Observations - by El Forum - 01-22-2008, 09:35 AM
My Codeigniter Observations - by El Forum - 01-22-2008, 10:01 AM
My Codeigniter Observations - by El Forum - 01-22-2008, 02:29 PM
My Codeigniter Observations - by El Forum - 01-22-2008, 02:59 PM
My Codeigniter Observations - by El Forum - 01-22-2008, 04:31 PM
My Codeigniter Observations - by El Forum - 01-23-2008, 12:00 PM
My Codeigniter Observations - by El Forum - 03-19-2009, 10:42 AM
My Codeigniter Observations - by El Forum - 07-13-2009, 07:19 AM
My Codeigniter Observations - by El Forum - 07-14-2009, 03:09 AM



Theme © iAndrew 2016 - Forum software by © MyBB