Home arrow PHP arrow Page 4 - Adding a Controller Class to an MVC-Based Framework in PHP 5

The partial source code of the user controller class - PHP

In this tenth part of the series, I start building a basic MySQL-driven application whose main tasks consist of selecting, inserting, updating and deleting records on some hypothetical users by means of the framework we've built.

TABLE OF CONTENTS:
  1. Adding a Controller Class to an MVC-Based Framework in PHP 5
  2. Using the MVC framework: setting up the database schema and building a controller class
  3. Implementing the controller's index() method
  4. The partial source code of the user controller class
By: Alejandro Gervasio
Rating: starstarstarstarstar / 2
June 29, 2010

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

The best way to understand the underlying logic of the controller class just created is by examining its source code in its current state. With that idea in mind, below I listed the partial definition of this class, naturally including the "index()" method that you learned before. Here it is:

class UsersController

{

private $model = NULL;

 

// constructor

public function __construct()

{

// store model object as property

$this->model = new Model(MySQL::getInstance(array('host', 'user', 'password', 'test')));

}

// fetch and display all users

public function index()

{

// create view object

$view = new View('users');

// create view properties

$view->title = 'Using the MVC design pattern with PHP 5';

$view->heading = 'User List';

$view->users = $this->model->fetchAll();

// display view

echo $view->display();

}

}

Definitely, I must admit that the controller class is starting to look a bit more functional. It's now capable of retrieving, via the model's API, all of the records contained in the "users" MySQL table. It's not possible to display them on screen yet, however, since this process requires the creation of a view file.

This additional file will be defined in the next tutorial of the series. In the meantime, feel free to tweak the code of the previous controller class, to make it slightly more useful.

Final thoughts

That's all for the moment. Over the course of this tenth part of the series, things became really exciting because I started building a basic MySQL-driven application whose main tasks consisted of selecting, inserting, updating and deleting records on some hypothetical users by means of the framework.

Right now, the application looks pretty skeletal, since it's only comprised of the MySQL database table containing some user-related records and a controller class that implements its "index()" method, which is used for fetching all of the users stored in the table and displaying this data on the browser.

Nonetheless, turning this sample application into a more functional piece of software is only a question of adding more methods to the controller class. Therefore, in the tutorial to come I'm going to do precisely that, thus enabling the controller to insert and update user-related records.

My final piece of advice is simple: don't miss the upcoming article!



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