Lynda. Up and Running with PHP CodeIgniter / Section 2 - 5. Generating output with a view

  • 10 years ago
Generating output with a view

A view is a partial or complete webpage. Designers like views because it's most HTML with a very bar minimum in PHP code, and what logic is there are four hs and so forth. Views can be embedded in other views, which allows for hierarchies and cleaner, more reusable code. Even though they're webpages, a view cannot be called directly. It can be only loaded by controller. So with this context, let's get that HTML out of the controller and into a view.

From NetBeans, I'm going to navigate to the Views directory. Right now, there's only two files. The same placeholder index.html and welcome message.php. Open welcome message.php to see what's inside. Scrolling through, it's a complete html five page with no php whatsoever. At the bottom there is one little anomaly. Page rendered in elapsed time in curly braces. This is a pseudo variable from the benchmarking class... I'll get into class libraries in a later chapter.

For the time being, just close welcome message. Then create a new file in views called magazines, plural, .php. And the magazine controller. Let's copy that HTML. And paste it into magazines.php. Next, we'll need to load the view. Take a look at the welcome controller for just a second, and see how it loads the view using this. What is this? This has a special meaning in CodeIgniter. It's a singleton CodeIgniter instance, which means there can only be one copy of it throughout the entire application, which prevents duplicates in overhead. When various functionality is loaded, it's attached to the instance, in such a way, that it can be referenced, like a property of the instance. I'll demonstate how this works in just a moment.

Close the Welcome controller and go back to the Magazine controller. We'll replace the echo with this, load and we're going to load a view. Named magazines. Followed by semicolon and save. I'm going to go back to the browser and refresh. Make sure that nothing has changed. It says, My magazines, again. Now there's no limit to the number of views that are loaded. If you chain them, they'll be appended. Let's experiment a little bit by going back to the magazine controller. And copying and pasting this load view.

And doing it twice. Save, then in the browser, refresh. Now there's two My magazines. If I view the page source. I can see that the only markup that is shown is what was in my view. See what I mean by coding neither having low overhead. None of the (UNKNOWN) fully formatted HTML documents. Wait a second. That's actually something I want. I'm going to get into more detail about how to set up a proper template in an outcoming chapter. Close the source, and go back to NetBeans. Let's clean up the controller by removing that extra line, and then save.

In this chapter, I've defined what CodeIgniter is, and gave a number of reasons why it should be used. I've introduced the Model View Controller development pattern. Then I installed CodeIgniter and tested the database configuration. Starting with some code and concepts, I created a static page controller and finally generated output using a view. At this point, we're ready to display content but we don't actually have anything to show. No worries, we'll sort that in the next chapter as we model magazines in the database.

Recommended