(created: November 14, 2014; last update: January 12, 2015)
OK… You are a bit knowledgeable regarding PHP, you are proficient at creating spaghetti code, and you know a thing or two about OOP. Then, someone comes to you and says something about MVC. You say “Riiiiight…”, and do your best not to betray the fact that you don’t know a darn thing about what MVC is.
So you go to Wikipedia and write “MVC” in the search form.
“Model–view–controller (MVC) is a software architectural pattern for implementing user interfaces. It divides a given software application into three interconnected parts, so as to separate internal representations of information from the ways that information is presented to or accepted from the user.”
“Wow… Why didn’t I think of that?”, you say to yourselves. After a bit of reading you find out that:
A controller can send commands to the model to update the model’s state (e.g., editing a document). It can also send commands to its associated view to change the view’s presentation of the model (e.g., by scrolling through a document).
A model notifies its associated views and controllers when there has been a change in its state. This notification allows the views to produce updated output, and the controllers to change the available set of commands. In some cases an MVC implementation might instead be “passive,” so that other components must poll the model for updates rather than being notified.
A view requests information from the model that it uses to generate an output representation to the user.
After that you can absolutely say “YES! I KNOW WHAT MVC IS!!!”. But how can you implement something like this in PHP? The easiest thing, you might say, would be to create your own MVC structure and start from there (a great tutorial about that would be PHP Academy’s). But after a lot of tries and fails you say: “F*** IT!” and look for a well established MVC framework.
If you do a Google search you will find a lot of alternatives. But I will assume that if you got to this page, you’ve found CodeIgniter to be of your liking.
So, download a fresh copy of CodeIgniter, put it inside your server’s public folder (or wherever you want it to be), and see if all went well.
Of course you can also download CI 2.2.4, but I would advise you to install version 3, even if it is in development stage.
For this tutorial we will put it inside htdocs (I will suppose that you have XAMPP installed).
Now, if you visit “http://localhost” you should see your new CodeIgniter “site”…
- Step 1.1: Again about MVC and CodeIgniter – The controllers
- Step 1.2: Again about MVC and CodeIgniter – The views
- Step 1.3: Again about MVC and CodeIgniter – The models
- Step 2: Set up the environments
- Step 3: Set up the database that will be used by CodeIgniter
- Step 4 – Removing the index.php from the URL and allow the use of “search-engine friendly” URLs
- Step 5 – Final settings. Finishing the configuration of your CodeIgniter
- Step 6 – Verify CodeIgniter configuration. Is everything OK?
- Step 7 – Using CodeIgniter and Composer – with a real world example
- Step 8 (optional) – Using sessions with database in CodeIgniter
- Step 9 – No more MY_Controller monopoly. How you can create more than one base controller
- Step 10 – Make your first library: a breadcrumb generator
- Step 11 – Creating and working with the famous MY_Controller
- Step 12 – Creating and using page templates in CodeIgniter
- Step 13 – Uploading multiple files (images) in CodeIgniter
- Step 14 – Crop and resize uploaded images in CodeIgniter
- Step …n…: Moving application and system folder to safety