I’ve been working on a super secret side project. I’ve been build it out with Code Igniter. which I absolutely love. I’ve mentioned before how much I love Expression Engine. Well, Code Igniter comes from the same company, Ellis Labs. The first time I “got” Code Igniter, I totally geeked out and literally started giggling.
People will ask me how I’ve taught myself to program. Code Igniter was a little different. I started digging around online and found a few articles. By piecing them all together, I was able to create a better whole. One of the reasons I love Code Igniter is because the documentation is so well written. However, I needed a resource to get me jump started, to give me a starting place. So, I thought I would share some of the things that I’ve learned. Hopefully this will mean one less Google for you (because you’ll find what you need here.)
Wrapping your head around Model-View-Controller (MVC)
This was the hardest part for me. Once I understood MVC, everything else fell into place quickly.
In programming, there are several different patterns for building and organizing that are supposed to make things easier, not only for you, but anyone else reading your code.
The idea behind MVC is to separate all the layers of code. Your database calls, one layer (model), what the user sees, another layer (view), and a layer that talks to everybody (controller). This is great because it means that presentation and design is separate from you database. Why is so great? A few reasons.
- Design is separate from your code. Meaning, if someone has to make changes, they won’t mess up your database.
- Or, my personal favorite reason, it’s so easy to reuse code. Smarter, not harder. Write it once, in one place, and be done with it.
Let’s dig a little deeper.
As I mentioned earlier, the model is the layer that talks to the database. It makes all your MySQL calls and returns what it finds.
Generally, each table in the database should have it’s own model. (Obviously, there exceptions.—I don’t have models for relationship tables)
Ellis labs has created some naming conventions. For models, all files are appended with _model.php This may seem fairly obvious, but just for kicks and giggles, if I had a comments table and a blog posts table, I would have 2 models: comments_model.php and blogposts_model.php
Even though the view doesn’t talk to the user directly, it has the pieces the user actually sees.
For example, if you have a standard HTML page, I’ll usually divide it into 4 different files. — Think about how you would set up php includes.
Now, if I have to make changes to the navigation or something in the footer, I only have to do it in one place. Smarter, not harder.
As I hinted in the diagram above, all view files are prepended with _view.php.
Before, you get mad at Ellis Labs for making decisions for you concerning file naming, it actually is quite beneficial. It helps me keep track of where I’m at in my code. If I’m creating a comments section for my blog, I’ll have a comments_model.php and a comments_view.php. Granted, they stay in separate folders, but I can easily tell by the file name what its purpose is.
This is the piece that does all the work. It asks the database a question, passes the answer to the view, and shows the user.
The file name for the controller is just its name. It’s not appended with _controller.php. Continuing with my blog analogy, the filename would simply be blog.php.
The controller name is actually part of the URL. For my blog.php? it would be index.php/blog or for an about page that was controlled by about.php, it would be called index.php/about
You can create a mod rewrite to control your URLS so the user doesn’t have to see the index.php part.
Earlier I mentioned other articles that I had found online. If you want to do some more reading, here are a few places to get started:
- CodeIgniter From Scratch: Day 1
- CodeIgniter and Doctrine from Scratch Day 1
- 40+ CodeIgniter Framework Tutorials for Kicking PHP Applications
- CodeIgniter Tutorial Links
- Everything You Need to Get Started with CodeIgniter