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.)
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.
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
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:
MVC is just working for me too and that is why I bow down for Ellis Labs, I enjoy my programming and the way I create my blog, how I viewphp comments. It is an understatement to say that it is smarter and not harder. – jordan