Home arrow PHP arrow Page 2 - An Introduction to Simulating the Model-View-Controller Schema in PHP

Defining the MVC schema's first element: constructing a basic PHP controller - PHP

Would you like to learn how to simulate an MVC-based system with PHP? If so, you've come to the right place. This is the first article in a three-part series that will show you how to build this schema in PHP by constructing a few classes that represent what is needed.

TABLE OF CONTENTS:
  1. An Introduction to Simulating the Model-View-Controller Schema in PHP
  2. Defining the MVC schema's first element: constructing a basic PHP controller
  3. Extending the MVC relationship: creating a basic model class
  4. Completing the MVC schema: defining the view component
  5. Assembling the respective elements: implementing the complete MVC schema
By: Alejandro Gervasio
Rating: starstarstarstarstar / 20
August 07, 2006

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

In order to start drawing the general structure of the Model-View-Controller schema (from this point onward, I'll reference it as MVC) that I plan to build, I'll provide you with a basic hands-on example, which can be appropriately understood with only minor complications.

In short, my example will be structured as follows: I'll build an object-based system, which will be capable of displaying simple messages in different ways, that is in lowercase, uppercase and reverse respectively. Although certainly this system won't have an immediate application in the real world, it will serve as a good introduction to how to construct a MVC relationship with PHP.

Having defined the basic structure of my MVC-based example, I'll begin defining the first element of this schema, in this case the controller itself. This component will be directly represented by a PHP class, which will be responsible for instructing the model (more on this in a moment, thus be patient please) on what types of views, that is lowercase, uppercase and reversed, will be properly generated.

The definition of the example looks really simple, therefore here is how the controller class, which I called "MessageController" will look:

// define 'MessageController' class (controller)
class MessageController{
    private $viewRanges=array('uppercased','lowercased','reversed');
    private $view;
    // constructor
    public function __construct($view='lowercased'){
        if(!in_array($view,$this->viewRanges)){
            throw new Exception('Invalid type of view!');
        }
        $this->view=$view;
    }
    // return type of view
    public function getView(){
        return $this->view;
    }
}

If you examine the signature of the above class, then you'll quickly realize what it does. Basically, this class accepts a type of view to be applied to the future model (as you know, in this example the model will be made up of simple message strings), and stores the inputted view as a new class property.

Aside from the corresponding checking code, pointed out to verify the validity of the view passed in as an argument, the class exposes the "getView()" method, which comes in handy for returning precisely the type of selected view to the calling code. Nothing unexpected, right?

As you'll probably agree, the panorama looks really simple when it comes to creating a controller class with PHP. Now, let's move on and see how a model class can be included as part of the MVC schema.

Based on the practical example that I'm currently developing, the model should be another PHP class, which would accept as an incoming parameter the controller object that you saw before, in order to display messages either in lowercase, uppercase or reversed. Now, do you see the logic followed by the MVC relationship? Great! Therefore, to see how the model class will be created, please click on the link shown below and continue 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: