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

The sample application's files - 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

As I promised in the previous section, here’s the list of files that compose this sample MySQL-driven application, this time including the corresponding user controller class and the pair of view files coded before.

First, here’s the controller:

(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

Now it’s time to show the first view file, so here it is:

(users.php)

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>

<title><?php echo $title;?></title>

</head>

<body>

<h1><?php echo $heading;?></h1>

<?php if (empty($users)):?>

<h2>Oops! There are not users in the database.</h2>

<?php else:?>

<?php foreach($users as $user):?>

<p><strong>First Name : </strong><?php echo $user->fname;?></p>

<p><strong>Last Name : </strong><?php echo $user->lname;?></p>

<p><strong>Email : </strong><?php echo $user->email;?></p>

<hr />

<?php endforeach?>

<?php endif?>

</body>

</html>

And finally, here’s the second view file; it displays an HTML form using the framework’s form helper:

(user_form.php)

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>

<title><?php echo $title;?></title>

</head>

<body>

<h1><?php echo $heading;?></h1>

<?php echo Form::open(array('action' => 'save', 'method' => 'post'));?>

<p>First Name: <?php echo Form::input(array('type' => 'text', 'name' => 'fname'));?></p>

<p>Last Name: <?php echo Form::input(array('type' => 'text', 'name' => 'lname'));?></p>

<p>Email: <?php echo Form::input(array('type' => 'text', 'name' => 'email'));?></p>

<p><?php echo Form::input(array('type' => 'submit', 'name' => 'send', 'value' => 'Create user'));?></p>

<?php echo Form::close();?>

</body>

</html>

This is it. At this point, you can see that building a basic database-driven application by using the sample MVC framework is indeed a no-brainer process that can be tackled with minor hassles. Of course, the functionality of each of its components can easily be enhanced, so go ahead and do it. It’ll be a good exercise for sharpening your programming skills.

Final thoughts

In this penultimate chapter of the series, I finished building a MySQL-driven application that used the functionality of the framework to perform CRUD operations on a database table populated with user-related data.

Even though it's pretty basic, this example demonstrated how easy it is to build applications like the one mentioned above by simply reusing the framework’s components. And speaking of components, the only one that remains unused is the Cache class. So, in the last tutorial I’m going to show you how to use this class within the earlier sample application to cache database result sets, thus improving its performance.

Don’t miss the final 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: