Front Controller Pattern

Setup

The following files are needed for this exercise..

Motivation

When a user interacts with an application, the handling of requests can occur through multiple parts of the application causing multiple objects and views increasing code duplication. The Front Controller offers a central access point for all requests which are commonly channelled through a handler that dispatches the request to the appropriate command.

UML Diagram

Front Controller Class UML diagram

Usage

In an application we only want 1 front controller, therefore the front controller will be a singleton.


namespace controllers;


use controllers\ApplicationController;
use base\Request;

require_once 'base/Request.php';
require_once 'ApplicationController.php';

class FrontController
{
  static private $_instance;

    private function __construct()
    {

    }

    static function GetInstance()
    {
        if(!isset(self::$_instance))
        {
            self::$_instance = new Self();
            ApplicationController::Init();
        }

        return self::$_instance;
    }

Using the Front Controller

As a central access point, the front controller uses a method called 'HandleRequest' to handle a request.

$frontController = FrontController::getInstance();
$request = Regsitry::getInstance()->getRequest();
$frontController->HandleRequest($request);

Implementation


    public function HandleRequest(Request $request)
    {

    }
Complete the HandleRequest() method in the Front Controller. This method should get the appropriate command, execute the command and render the appropriate view.

Hints

Hint 1 (Click To View)

Hint 2 (click to View)

Hint 3 (click to View)