Home arrow PHP arrow Page 4 - Handling Views with CodeIgniter

Building a web page controller class - PHP

The Code Igniter framework makes it easy for developers to implement a Model-View-Controller design pattern in PHP. This concept can be difficult for newcomers to grasp, so this seven-part series of articles will focus on one aspect of it: views, and the many clever ways you can handle them with Code Igniter. Welcome to the first part.

TABLE OF CONTENTS:
  1. Handling Views with CodeIgniter
  2. Preparing views to be rendered on screen
  3. Creating a content view
  4. Building a web page controller class
By: Alejandro Gervasio
Rating: starstarstarstarstar / 9
March 19, 2009

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

To be frank, generating an entire web document by sequentially loading the three views that you saw before is a no-brainer process that you’ll grasp in a snap. But how can it be done? Well, first it’s necessary to define a controller class that performs this specific task. In this case, since I’m attempting to create a web page, the controller will be called “WebPage” (my creativity with names sometimes blows me away) and it’ll look like this:


<?php

class WebPage extends Controller{

function WebPage(){

// load controller parent

parent::Controller();

// load libraries here

$this->load->database();

// load helpers here

}

// load views sequentially

function index(){

// load 'header' view

$this->load->view('header_view',array('header'=>'Header Section'));

// load 'content' view and pass in database content

$this->load->view('content_view',array('users'=>$this->db->get('users')));

// load 'footer' view

$this->load->view('footer_view',array('footer'=>'Footer Section'));

}

}

?>


See how simple it is to build a web page by loading views in a sequential manner? The controller defined above demonstrates this process pretty clearly, since it firsts loads the CI’s database class within its constructor, and then  proceeds to build the different sections of the page by loading in turn each of the previous views. Simple and efficient.

Now that you hopefully understood how the “WebPage” controller works, save it under the “/application/controllers/ folder and type the following URL on your browser’s address field to test it: http://localhost/codeigniter/index.php/webpage/


If everything goes well, you should get the following output on screen:



There you have it. At this stage you've learned how to sequentially load three different views to generate a basic web page. Logically, there’s plenty of room to improve the signature of the controller or even the views themselves. But guess what? This will be left as homework for you.

Final thoughts

In this first episode of the series, I explained how to build different sections of a basic web page with CodeIgniter by sequentially loading three independent view files. It could be said that this approach is possibly the simplest to implement when handling views with CI, but as I said in the beginning, it’s not the only one.

In the next article, I’ll be polishing the visual appearance of the web page generated previously to make it look slightly more appealing. Now that you’ve been warned about the topic that will be covered in the forthcoming part of the series, don’t miss it!



 
 
>>> More PHP Articles          >>> More By Alejandro Gervasio
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: