Home arrow PHP arrow Page 3 - Building a Blogger with the Code Igniter PHP Framework

Building a blog controller class to display blog entries - PHP

People love to communicate, which may be why one of the most popular web applications you can develop is a blogger. In this six-part series, you'll learn how to develop a blogging application using PHP and the Code Igniter framework. In this first article, we'll create the bare bones structure, with plenty of code samples to help you understand the process.

TABLE OF CONTENTS:
  1. Building a Blogger with the Code Igniter PHP Framework
  2. Start building the blogger with two MySQL database tables
  3. Building a blog controller class to display blog entries
  4. Defining a simple view file
By: Alejandro Gervasio
Rating: starstarstarstarstar / 8
December 10, 2008

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement


As I anticipated in the previous section, the workhorse of this blog application will be represented by a controller class. This class will handle all of the tasks required by the program, such as listing all the existing blog entries and their corresponding comments, as well as displaying an HTML form that will let users to submit new posts.

However, for now let me show you the initial version of this controller, which momentarily will be tasked with displaying all of the blog entries stored on the "blogs" MySQL table. Here's how this class looks:


// define 'Blogger' controller class


class Blogger extends Controller{

function Blogger(){

// load controller parent

parent::Controller();

// load database class and connect to MySQL

$this->load->database();

// load helpers

$this->load->helper('url');

}

// display all blog entries

function blogs(){

$data['title']='Blog Entries Listing';

$data['result']=$this->db->get('blogs');

// load 'blogger_view' view

$this->load->view('blogs_view',$data);

}

}


As illustrated above, the "Blogger" controller performs a few useful tasks that deserve a closer analysis. First, its constructor loads Code Igniter's database class, and connects to MySQL by using the settings of its "config.php" file (for more information on how to install and configure Code Igniter, please read the user manual).

Once the database class has been included and it is working correctly, the constructor finishes its execution by loading the "url" helper function, which will be used later on for generating dynamic links.

Now that you have grasped how the controller's constructor does its business, it's time to look into the "blogs()" method, since its implementation is pretty interesting. As you can see, it first fetches all of the blogs entries from the previous "blogs" MySQL table and embeds this data directly into a view file, called "blogs_view.php," to be displayed on screen.

In case you didn't know, this is the approach utilized by Code Igniter to implement the MVC pattern, so if you're not familiar with this concept, please check the framework's user manual.

Before I move on and continue building this blog application, the previous controller should be saved to the Code Igniter /system/application/controllers/ folder as "blogger.php," so that it can be invoked correctly.

Done? All right, I have now built a controller class that's capable of displaying on the browser a list of blog entries. Well, actually, this isn't entirely accurate, since there's still an additional step that must be taken.

Yes, you guessed right! As I explained before, the controller loads a view file to display all of the blog entries, meaning that it is first necessary to create this file. Therefore, in the section to come I'll be showing you the details of this process.

Please click on the link below and keep reading.



 
 
>>> 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: