Home arrow PHP arrow Page 4 - Adding More Methods to the Controller Class of an MVC-Based Framework

The full definition of the controller class - PHP

In this eleventh part of the series, I expand the initial functionality of the user controller class defined previously by adding two simple methods to it. These allow it to create new users in the associated MySQL table.

TABLE OF CONTENTS:
  1. Adding More Methods to the Controller Class of an MVC-Based Framework
  2. Review: the initial definition of the user controller class
  3. Creating new users and updating existing ones
  4. The full definition of the controller class
By: Alejandro Gervasio
Rating: starstarstarstarstar / 1
June 30, 2010

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

To give you a clearer understanding on how the user controller class functions, below I've displayed its source code, including the two methods discussed in the previous section. Here's the class:

<?php

class UsersController

{

private $model = NULL;

 

// constructor

public function __construct()

{

// store model object as property

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

}

// 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();

}

// create new user

public function create()

{

// create view object

$view = new View('user_form');

// create view properties

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

$view->heading = 'Create new user';

// display view

echo $view->display();

}

// update existing user

public function update($id)

{

$this->model->save(array('fname' => 'My first name', 'lname' => 'My last name', 'email' => 'myemail@domain.com'), (int)$id);

}

}

There you have it. Now that this user controller class is becoming more functional, you'll realize how easy it is to build MVC-based web applications using this small framework. Of course, there are some things left that need to be accomplished, such as coding the view files that some methods require. However, there's no need to feel concerned, since they'll be properly tackled in the next part of the series.

Final thoughts

In this eleventh part of the series, I expanded the initial functionality of the user controller class defined previously by adding a couple of simple methods to it  that allow it to create new users in the associated MySQL table.

Despite the basic structure of this class, it's handy for demonstrating how easy it is to plug in to different components of the framework, such as the database driver and the input class, as well as the model and the view class.

As you saw before, the methods responsible for fetching users and creating new ones use a couple of additional view files that haven't been defined yet. Therefore, in the next tutorial I'm going to code these files. Additionally the controller will be given the ability to delete users.

There are many things ahead of us that need to be learned, so don't miss the upcoming part!



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