Page Controller vs Front Controller

I am trying to build an E-commerce site from scratch, and would like to know…

Why would I want to use the Page Controller vs Front Controller as part of implementing the MVC framework??

(Honestly, I don’t really have a good feel for what the Controller does in the MVC, so reading about both of these is just more confusing to me?!)


To confuse matters further, the page controller and front controller are competing strategies, but neither of them have to do with MVC, or with the controller element of MVC. Briefly, page/front controller are strategies for bootstrapping the handler for a request in your application. What happens after the bootstrapping, may be MVC or not.

Used in conjunction with MVC, the Front Controller acts as the bootstrapper that uses the current request to figure out which Controller to load and execute. For example, with “GET /events/view/213” the Front Controller might determine to load the “Events” controller and call the “view” method with a parameter of “123”. It will probably use some sort of URL router to parse the URL, usually either just a simple explode on “/” or a more complex regex router (a good debate thread on the subject). The front controller may also handle errors (controller or method doesn’t exist, etc), wrap a layout around the response, format it a certain way, etc.

The Page Controller is more of a generic concept of an actual webpage that is served. A good usage example is a CMS. With a CMS, there is a very clearly defined concept of what a page is. You make new pages, they follow a hierarchy (parent/child pages), you set titles, descriptions, contents, etc. So a page controller might be a good design choice for something like this.

The two concepts have some overlap, but may not necessarily compete, depending on what your design goals are. If you plan to incorporate loose CMS-style pages along with other MVC design (like a shopping cart that also allows you to create content pages), then you may end up using both together. In that case, your Front Controller will still probably handle the request first, but then it might hand off the request to the Page Controller if it cannot satisfy the request, or if it recognizes the URL as a CMS page.

All the standard Controllers do is handle the request and generate a response. Most of the time that just means selecting a View or template to display with certain data when a certain action is called. For example, “/products/index” might load a “Products” Controller that calls the “index” method. It would then grab a list of products from the database and select a template to display the results in a grid view with image thumbnails, etc.

I know it’s a lot to take in when you’re learning, but hang in there - most of the concepts are fairly simple - the names often make the design concepts sound larger or more complex than they really are.

Thanks for the replies, kyberfabrikken and Czaries.

So, if Front Controller and Page Controller are not the same as the “Controller” in MVC, then can I survive without either in the beginning?

And what do you mean by “bootstrapping”?! :-/

Can you guys help me out with MVC? I spent all weekend reading, but there just aren’t any really good explanations online.

I have LOTS of specific questions and could use some coaching on implementing my first MVC… sigh



You also need to decide how you want the system to work and the features required, as an implementation of MVC can simply be…


function getProduct($id){
   you're hiding how you get the
   product information/data here.
  return array(
    'id'    => 4,
    'name'  => 'Foo Pillow',
    'price' => 45.90
$product = getProduct(1);
include 'views/product.xml.tpl';


<?php header('Content-Type: text/xml'); ?>
  <product id="<?php echo $product['id']; ?>" name="<?php echo $product['name']; ?>" price="<?php echo $product['price']; ?>" /> 

The best way to learn MVC I would say is to learn a framework and read a couple good design pattern books. Implementing MVC from scratch without having ever used it first would be a pretty big task.

Are you allowed to use a framework in your project? Once you’ve built a couple MVC projects using a framework, you would have a better idea how to implement your own little MVC framework.

That’s kind of where I’m at now. I’ve been using Zend Framework for a couple months now and I’m getting ready to write my own much simpler framework. I’m not familiar myself with any other frameworks, but I hear others are a lot easier to learn than Zend. I hear good things about Symphony.

Or if someone wants to be nice and work one-on-one with me for a bit… :wink:

Are you allowed to use a framework in your project? Once you’ve built a couple MVC projects using a framework, you would have a better idea how to implement your own little MVC framework.

I’m trying to build my own e-commerce site, so I am the boss and can do as I see fit. (I’d like the satisfaction - and knowledge - of building things from scratch.)


First things to remind: I’m giving really simple examples that don’t match every app - as they are just what they are: examples.
So please don’t take every of my words as carved in stone.

As mentioned above, both are not what is called a controller in MVC - the name of the patterns might be a little irritating.
A controller is, in most MVC apps, a class that has some “action” methods.

Your app gets a request for /products/details/123
The front controller takes this request and extracts these details: controller “products”, action “details”, parameter “123”
After that, it calls the class “ProductsController”, respectively it’s action method “details” and passes the parameter “123”.
So the front controller is your starting point (thus: bootstrap) for each and every request - it decides which class (controller) is responsible for handling the request.

Bootstrapping, in general, means “preparing the environment”, e.g. defining some paths, setting up the database connection, parse the requested URL and then pass over to the controller.

Well, MVC and all it’s relating patterns isn’t a simple thing, so you shouldn’t just watch out for online tutorials.
A good starting point, but a heavy one as well, might be “Patterns of Enterprise Application Architecture” by Martin Fowler.
The book explains many, and I mean many, patterns that are useful when building large scaled apps.

HTH, if not, keep asking :wink:

Sounds like you need to pick yourself up a copy of PHP in Action Objects, Design, Agility by Dagfinn Reiersol, Marcus Baker and Chris Shiflett. It will answer your questions about MVC, Page Controller vs Front Controller, design patterns etc. It’s a very practical book and easy to understand.

The author(s) recommend a certain reading order but I’m reading just the bits I need without any confusion.

After using CodeIgniter for about 2 years, I’ve finally bitten the bullet and looking at writing my own too.

The “Front Controller” is simply what ever parses the request and then executes the Controller methods based on that.

A very simple method of this is:

    DEFINE('MVC', 1);

    class MVC
         * Determines the route in which the user wants to take
         * and attempts to locate and execute the required
         * controller and action.
        public function Go()
            $Parts = $this->GetParts();
            $Controller = ucfirst(strtolower(array_shift($Parts)));
            $Action = (empty($Parts) ? DEFAULT_ACTION : array_shift($Parts));

                $x = $Controller.'_controller';
                $C = new $x();
                $C->Instance = $this;
                if(!($C instanceof Controller))
                    $this->Error('Error, controller has not been derived from the Controller class');
                if(method_exists($C, $Action))
                    call_user_method_array($Action, $C, $Parts);
                    $this->Error('Error, action could not be found');
                $this->Error('Error, controller could not be found');

         * Splits the current request url into an array
        public function GetParts()
            $Parts = explode('index.php', $_SERVER['PHP_SELF']);
            $Parts = explode('/', trim($Parts[1], '/'));
            if(empty($Parts) || empty($Parts[0])) $Parts = array(DEFAULT_CONTROLLER);
            return $Parts;

         * Handles errors passed through the framework
         * @param string $ErrorStr The error to display
         * @param string $Type The type of error
        public function Error($ErrorStr, $Type = 'Fatal')

     * Create an instance of the MVC framework
     * and process the current request.
    $Instance = new MVC();


The above code would create an instance of the controller your requesting and then the action/function within. If you don’t specify either a controller or action, the script has access to default’s which would be your default route.

Edit: Btw, I know I could use things like AutoLoad etc, I have just been playing around.