Learning About Views in Model View Controller

codeigniter-views

In the MVC structure we separate out the logic from presentation. The controller which we explained in a prior blog post handles the processing and incoming http requests. The controller is the boss sort to speak. When you make a request to the website, that controller starts making some decisions. It asks, “What is it, that this user needs or wants?”. You tell the controller this, whether knowingly or not, based on the actual URL that you are requesting. This determines, what methods get called within the controller classes, which then in turn determine what models to lean on in order to get some business data and logic, and finally we pass this valuable data to the View, which our ninja web designers with their awesome front end skills will make look nice and presentable. What follows is some info on how the View works.

What is a View?

A view in MVC is where your markup language resides. Intermingled in that HTML will be simple PHP variables and potentially some basic logic, although we want to put any intensive processing in the Controllers and Models. Our Controllers and Models will typically contain really nice, concise, and structured PHP, perfectly indented and following best practices. The View presents this markup as a webpage to the user, with fields being updated and changed using PHP variables. Your Views may be slightly more Spaghetti like, which obviously we would like to minimize, however if it weren’t for MVC, your entire project may be like a bowl of pasta.

Your First View

Let’s take a look at what an example view might look like:

Your view files will follow conventions such that they will share similar naming as their associated controller which may call or load them. Our Controllers In Codeigniter article gave an example of a controller named Sayhi. Recall that the code looked like this:

In that example we simply echoed out ‘I will now say hi!’ to the screen. This is not best practice and you typically would hand over the data from the controller over to the View, so that the view can do the presentation. So how could we accomplish this? Well, take the html snippet above and save it to a PHP file in your application/views folder. You would likely name it sayhiview.php in following the naming conventions. Let’s change it up and now and instead of echoing out a statement, we’ll load our newly created view file. This is how we can do that:

Now when you run http://www.site.com/index.php/sayhi/ It will load the view file you created instead of saying hi.

Pass Variable Data from Controller to View

Well that was fun you might say, but that view doesn’t do all that much. How can we put some data in the controller and then have the view show that data dynamically? You can accomplish just that by doing the following:

We have updated our controller to contain some variable data and placed that data into the $data array. You can pass data in the form of an array or object to the view. Now you can update the view and replace the hardcoded information with PHP variables that correspond to the keys of the $data array we just created in the controller. Since we have keys of 'title', 'heading', and 'sayit' in our $data array, we now have the PHP variables of $title, $heading, and $sayit in our view. Pretty cool, right?!

Getting Loopy With Views

The examples so far are actually not very common in that you wouldn’t statically set the array keys of $data in your controller. It would typically get populated via a call to a database that would return multiple rows of information. You can assign an array to one of the array keys as well. Consider:

This iteration is pretty cool. In our controller we assign an array containing the values of PHP, Blogging, and WordPress, to the $data['interests'] key of our $data array. We now have an array within an array, also known as a multidimensional array. You will see the multidimensional array with amazing frequency during your PHP programming, both in Framework environments and natively so it pays to become familiar with them.

What happens now when we run http://www.site.com/index.php/sayhi/ is for the Sayhi controller to get called, we populate three elements of the $data array with strings, and the fourth with an array, and finally we load the sayhiview.php while passing the $data array to it.

In the view, we simply echo out the first three string values, and then iterate over the contents of the fourth key of our $data array, and print an unordered list of interests to the user. The foreach construct takes the $interests array and places each value of the array elements into a single $interest variable on each iteration. We then simply echo out each interest, one iteration at a time.

Thank you for reading Learning About Views in Model View Controller – If you found this post helpful, Please do share using the buttons below!