Home arrow PHP arrow Page 2 - PHP MVC Frameworks: Building View Files

Review: the user controller class - PHP

In this next-to-last part of the series, I finish building a MySQL-driven application that uses the functionality of the framework we've created to perform CRUD operations on a database table populated with user-related data.

TABLE OF CONTENTS:
  1. PHP MVC Frameworks: Building View Files
  2. Review: the user controller class
  3. Generating browser output with view files
  4. The sample application's files
By: Alejandro Gervasio
Rating: starstarstarstarstar / 2
July 07, 2010

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

Before we start coding the view files required by the user controller class created in the previous tutorial, it’d be useful to recall the definition of this component of the framework. This way, you can see how easy it is to build an application that sticks to the MVC design pattern.

The file containing the definition of the user controller mentioned in the introduction was built like this:

(UsersController.php)

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

}

 

// save user

public function save()

 {

// get POST params

if (Input::post('send'))

{

$fname = Input::post('fname');

$lname = Input::post('lname');

$email = Input::post('email');

// save user data

$this->model->save(array('fname' => $fname, 'lname' => $lname, 'email' => $email));

}

}

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

}

// delete existing user

public function delete($id)

{

$this->model->delete((int)$id);

}

}// End UsersController class

As you can see, the user controller class is comprised of a few straightforward methods that allow you to retrieve, insert, update and delete records on a MySQL table populated with data on some fictitious users via the model’s API, including their first and last names, and their email addresses as well.

Of course, due to the inherent flexibility offered by the model, switching between different database tables is a breeze, but for the illustrative purposes of this tutorial I’ll keep using the table that stores only user data.

So far, so good, right? At this stage, you understand the underlying logic of the previous controller class, which also makes use of other components of the framework, such as the input and view classes, to do its business.

However, the instantiation of view objects within the controller implies a direct inclusion of view files that output something to the browser. In this case, the process occurs in the “index()” and “create()” methods respectively. Obviously, it’s necessary to define these files to get this sample PHP application working as expected, so in the following section I’m going to do precisely that.

Now, to learn more about this process, click on the link that appears 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: