ASPit - Totally ASP JSit - Totally JavaScript
Search PHPit

Use this textbox to search for articles on PHPit. Seperate keywords with a space.

Advertisements

Building a simple MVC system with PHP5

(Page 4 out of 5)

The Controller

The controller part of our MVC system is actually very simple, and requires very little work. First, make sure that the 'controllers' directory exists. Then, create a new file called 'controller_base.php' in the 'classes' directory, and put the following code in it:

Abstract Class Controller_Base {
        protected $registry;

        function __construct($registry) {
                $this->registry = $registry;
        }

        abstract function index();
}

?>

This abstract class will be the parent class for all our controllers, and it does only two things: saves a copy of the Registry class and makes sure that all our controllers have an index() method.

Now let's create our first controller. Create a new file called 'index.php' in the 'controllers' directory, and add the following code to it:

Class Controller_Index Extends Controller_Base {

        function index() {
                echo 'Hello from my MVC system';
        }

}

?>

We've now created our first controller, and if you try to run our MVC system now, you should see the following:

(Click here to view full version)

This means that our Router class did the job, and executed the correct controller and action. Let's create another controller that corresponds to a request that looks like 'members/view'. Create a new file called 'members.php' in the controllers directory, and add the following code to it:

Class Controller_Members Extends Controller_Base {

        function index() {
                echo 'Default index of the `members` controllers';
        }

        function view() {
                echo 'You are viewing the members/view request';
        }

}

?>

Now go to your MVC system, and make sure that the request is 'members/view', either by directly going there or by going to index.php?route=members/view. You should get the following result:

(Click here to view full version)

Just by creating a new controller class and adding a method we've been able to define a whole new page in our MVC system, and we didn't have to change anything else in our system. Our controllers don't need to include a 'global.php' file or anything like it whatsoever.

Now that we've got the controller part working in our MVC system, there's only one thing left: the 'V' or View part of our MVC system.

« Previous: The Model & Router Class
Next: The View & Security Measures & Conclusion »



4 Responses to “Building a simple MVC system with PHP5”

  1. Marion DESNAULT Says:

    Hi,
    thanks for this useful article about the MVC system.
    Just to report a problem using function trim in the Router’s setPath method (line 14) under UNIX systems.
    If we give it an absolute path, it will delete the leading / and create problem when checking if this path is a directory.

  2. Solid Says:

    Why you haven’t used the magic overloading methods __set and __get, instead of using registry class? It’s more easy to use __set, __get with static propery, because in future you don’t need to use methods get()/set(). Anyway, I’m using something like you have been described here for compatibility with PHP4. Instead of class static property I’m using container function, because in PHP4 there are no chance to declare static property in class body.

  3. Alexander(Russia) Says:

    Hello, good article about the MVC for starting.

  4. Matthijs Says:

    Pretty cool article Dennis. Thanks. Good to see a relatively easy explanation of an MVC system. The thing with MVC is that the basics is fairly simple. But it can get complicated quickly when you throw in some locators, observers, datamappers and other classes and patterns.

Leave a Reply

About the author
Dennis Pallett is the main contributor to PHPit. He owns several websites, including ASPit and Chill2Music. He is currently still studying.
Article Index
  1. Introduction & One Point of Entry
  2. Startup Tasks & Registry Class
  3. The Model & Router Class
  4. The Controller
  5. The View & Security Measures & Conclusion
Bookmark Article
Download Article
PDF
Download this article as a PDF file