Professional CodeIgniter

(singke) #1

Chapter 1: Welcome to the MVC World


16


}

function index(){
$this- > load- > model(‘Page_model’,’’,TRUE);
$data[‘content’] = $this- > Page_model- > fetchHomePage();
$this- > load- > view(‘home’,$data);
}
}
? >

When you look at this controller, notice how organized it is, and how easy it is to figure out what is
going on. Any visitor to the site ’ s index or home page will kick off a discrete process: Invoke the model,
retrieve the data, and display the view with data. There ’ s nothing hidden about any of it; there are no
other configurations to look up. Simple, clean, tidy.

Creating the View


The final step, creating the view, is quite possibly the easiest. Simply create the appropriate file in the /
system/application/views/ folder (in this case, a file called home.php ):

< html >
< head >
< title > < ?php echo $content[‘title’];? > < /title >
< link href=” < ?php echo $content[‘css’];? > ” rel=”stylesheet” type=”text/css”/ >
< meta http-equiv=”Content-Type” content=”text/html; charset=UTF-8”/ >
< meta name=”keywords” value=” < ?php echo $content[‘keywords’];? > ”/ >
< meta name=”description” value=” < ?php echo $content[‘description’];? > ”/ >
< /head >
< body >
< h1 > < ?php echo $content[‘title’];? > < /h1 >
< p > < ?php echo nl2br($content[‘bodycopy’]);? > < /p >
< /body >
< /html >

Please note that the $content[ ‘ css ’ ] in this code example is just a file name! You ’ re not storing the
entire CSS file in the database. You could if you wanted to, but it makes more sense to keep this
information in a separate file.

Notice that the view is mostly HTML markup with PHP commands interspersed. The view contains a
title tag (with dynamic content pulled in from the database), a dynamically inserted CSS filename (again,
from the database), and other information, such as the UTF - 8 charset declaration. You may be wondering
what the point of all this is if you ’ re eventually going to have PHP mixed in with your HTML. It ’ s
important to understand that :

You have a lot less PHP in your HTML at this point.

All the important business logic and data extraction have occurred in the controller and model.

Because the view is PHP, it can be cached by CodeIgniter or by another server - side optimization
process.



Free download pdf