Getting Started With CodeIgniter - 5
If you're new here, you may want to subscribe to my RSS feed. Thanks for visiting!
One Controller Per View in CodeIgniter
So how do you break a website into views and controllers exactly? If you read the CodeIgniter forums people have devised lots of different ways of tackling the controller problem.
For me, taking the project I am currently working on, I find it easy enough to think in terms of objects for the tours, the shopping cart, a customer and so on. But the static web pages - the views that end up being your about, contact me, terms and conditions and privacy pages - how do you break down the controllers for those?
This is an especially testing question because you are likely to hit it pretty quickly when you decide to learn CodeIgniter and the MVC approach at the same time. You’re likely after all to want to start with simple pages.
First I thought - I’ll do one controller per view no matter how straightforward the page content. Then as soon as I had decided that, doubts crept in. No way! That’s absurd. How can you have one controller for the about page, another for the company history page and yet another for the terms and conditions page. They’d be practically the same!
Changed My Mind - One Catchall Controller for All Static Views
So I set about creating a common controller that would control all static pages. By static, I mean pages that only contained text that would rarely change.
Hmm - what was I going to call it - maybe the content controller (content.php), or maybe the static controller (static.php)? I was having trouble finding a good name for this catchall controller. This seemed to me like a sign I was on the wrong track.
The next thought that came to mind was - well hang on, I might want to add something data flavoured to the About page one day - I don’t know - it might be a feed of the client’s Twitter tweets. What then, the about page would suddenly no longer be hopelessly static and if I’d had the foresight to create one controller per page at the start, I wouldn’t have to change the general content controller for all static pages, just because one needed more.
Back to One Controller Per View
I am now back to the one page per controller idea. This meant however that each controller for each static page, necessarily contained a lot of repeated code. I hunted through the CodeIgniter forums for the best way to address this, and the answer appeared to be to use a helper.
I say ‘appeared’ as the CI forums can get very technical and sometimes, I am not not sure what they are all talking about. Hence the need to write this stuff on my blog I suppose.
Here’s a list of forum posts that I found useful whilst getting my head around this.
Using A Helper for Common Controller Code
If you decide to use one controller per view, you can cut down on repeated code by putting the common controller set up code (data loads etc) into a specially written helper. I wrote one called commoncontrol_helper.php and put it in the application/helpers directory. My helper looks like this, and contains (currently) one function called setup_assets. This follows on from my previous posts in this series.
And consequently, a controller that uses this helper will look like this :
Related Posts
- Getting Started With CodeIgniter - 4
- Getting Started With CodeIgniter - 3
- Getting Started With CodeIgniter - 1
- CodeIgniter MVC - How To Display Single Database Value In View
Tagged as: CI, codeigniter, tutorial
|
Lindsay Lohan's Assistant Worries Lindsay Will Kill Herself
In yet another recording released by Lindsay's dad Michael, her assistant Jenni Muro says, "I am trying to save your daughter's life every day." Wait, Lindsay Lohan has an assistant?
|
|
Twilight’s Christian Serratos Gets Naked For PETA
Serratos poses naked for the 'I'd Rather Go Naked Than Wear Fur' campaign.
|
|
100 Best Bikini Bodies
Click here for the best way to spend 10 minutes.
|







