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.
August 24th, 2006 at 5:58 pm
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.
August 28th, 2006 at 12:21 pm
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.
August 29th, 2006 at 9:38 am
Hello, good article about the MVC for starting.
September 4th, 2006 at 5:09 pm
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.