Getting Started With CodeIgniter – 3

Model – View – Controller

CodeIgniter implements the MVC (model – view – controller) design pattern. Writing web pages this way can help to separate how a page looks, from its data and also from its flow of control.

Views

When you write a web page in CodeIgniter you are encouraged to place all the visible stuff – the way the page looks, into a view page.

Models

Next you do your best to place all the data related stuff into a model page. This doesn’t just mean data in databases, but can also mean anything that counts as data on the page.

You can stretch this to encompass almost anything and you’ll see that I use a model to pass the name of any CSS and JS files I use, to a page.  I mean, you could just hard-wire those things directly into a view, but I have chosen not too.

Controllers

And you are supposed to place all the flow of control, or processing stuff, inside a controller page.

There are no hard and fast rules for doing this, and the way I’ve structured my web pages for the MVC approach may be frowned on by some.  However, my purpose in showing you how I do it here, is to get you started. Once you’ve seen how I tend to structure things you can makes your own changes and improvements.

Views and Controllers

I’d like to say a bit more about these. If you’re writing a program to send a rocket to the moon, and you’re writing it using object oriented techniques, you’d write a bunch of classes to represent the objects in your program.  You might for example, write the class rocket, and the class moon.

You ‘d probably then write methods (functions) like rocket.launch and have properties like moon.temperature. In fact everything (in other words all functions and data) you ever want to calculate or say about moons or any number of rockets will be encapsulated in the moon and various rocket objects.

The whole idea of OO programming is to keep relevant coding material in once place, to encourage strategic thinking about programming design, make code re-use come naturally, encourage clarity and to discourage the computing scourge of the 1970s, spaghetti code.

But What About Ordinary Web Pages?

But in web design, controllers and models are also classes and as such we should try to split the various classes up to somehow reflect the world.  I found this to be a tough call when using CodeIgniter for a business web site. After all, what are the objects when all you have to start with is a few web pages?

You could argue that CodeIgniter is an overkill for small sites, but to my mind, small sites have the potential  to grow so you may as well start out the way you mean to carry on . . .

MVC for Your Web Site

I assume that when I need to add the real database functionality the objects will be obvious, but in order to set up the basic, (practically) database free site, I create a class for each page. Yes – that means I have a  contact class and an about me class and a home class . . . .

In my next post, Getting Started With CodeIgniter – 4, I’ll list everything I do to get a home page up and running using the MVC model.

Comments

  1. Thank you very much, Liz.
    I’ll continue to follow your tutorial trying to set up my first site in CodeIgniter… 🙂

  2. I have fixed the pages so they should be visible now.
    Liz

  3. Hi Ibassa – I will fix this. Sorry for the problems. The site design was far too heavy so I’ve deleted it. I will see what I can do to restore missing posts this evening. Liz

  4. ilbassa says:

    Hi, i’m following your tutorial setting up my first site with a php framework.
    I think this step-to-step tutorial is well written but I have some problem to visualize two posts: “Getting Started With CodeIgniter – 4” and “Getting Started With CodeIgniter – 5”, the page doesn’t load completely so I can’t go through the entire post.
    Could you fix that pages?
    Thanks a lot,
    ilbassa