In the final part of this series, I will build a functional modular front controller that can be easily adapted to small projects. It is basically the “C” in MVC for a simple MVC framework. If you haven’t read the first two articles I recommend that you read over them first. Url Routing with PHP – Part One and Url Routing with PHP – Part Two.
Requirements
- Apache with mod_rewrite enabled
- PHP
A few changes
If you have followed along with the series you will see a few changes to the code that I left you with in Url Routing with PHP – Part Two. The Axial_URLInterpreter and the Axial_Command object received a few minor changes suggested via comments on the previous articles. Most of the changes are in the Axial_CommandDispatch class. A new class Axial_Controller is introduced in this article, along with some changes to the directory structure.
Dispatch via convention
Using a front controller allows creation and access to controllers in a standard way. By using a consistent naming convention and directory structure, command objects can be dispatched via a generic piece of code. When a command is dispatched the appropriate controller is loaded and its execute() method called.
-
class Axial_CommandDispatcher
-
{
-
var $Command;
-
-
function Axial_CommandDispatcher(&$command)
-
{
-
$this->Command = $command;
-
}
-
-
function isController($controllerName)
-
{
-
{
-
return true;
-
}
-
else
-
{
-
return false;
-
}
-
}
-
-
-
function Dispatch()
-
{
-
$controllerName = $this->Command->getControllerName();
-
-
if($this->isController($controllerName) == false)
-
{
-
$controllerName = ‘error’;
-
}
-
include(‘axial/controllers/’.$controllerName.‘/controller.’.$controllerName.‘.php’);
-
$controllerClass = $controllerName."Controller";
-
$controller = new $controllerClass($this->Command);
-
$controller->execute();
-
}
-
}
As you can see from the code above on line 31, the controller must reside in a directory under ‘axial/controllers/controllername and be named using the convention controller.controllername.php.
The Controller
All controllers must descend from the base class Axial_Controller. The execute() function in the base class searches for the function passed in the Axial_Command object prepended with a ‘_’ within the controller, if this method is found it is executed if it is not found the _error() function is called, if no function is specified the _default() function is called.
-
class Axial_Controller
-
{
-
var $Command;
-
function Axial_Controller(&$command)
-
{
-
$this->Command = $command;
-
}
-
-
function _default()
-
{
-
-
}
-
function _error()
-
{
-
-
}
-
function execute()
-
{
-
$functionToCall = $this->Command->getFunction();
-
if($this->Command->getFunction() == ”)
-
{
-
$functionToCall = ‘default’;
-
}
-
-
{
-
$functionToCall = ‘error’;
-
}
-
-
-
}
-
}
Try it out
You can visit examples.phpaddiction.com/urlrouter/part_3/ to see it in action and download the sample code.
Conclusion
If you look at the index.php from the sample code in the second part in the series you will see that the code is virtually unchanged from this version. The main change is in the Axial_CommandDispatcher class and with the addition fo the Axial_Controller class I have build a pluggable extendable front controller based framework. Take a look at the example page and code to get a feel for what you can do with this mini framework.
When I began this series I left out any discussion of the view portion of MVC since it was beyond the scope of the title and what I intended to discuss, I discovered though that in order to build any further on the front controller portion of the framework I would need to introduce that concept. So I’m going to stop this series here and will pick it up again later together with a more fitting title and topic.
APR

About the Author