Simple calculator in PHP utilizing MVC I have been struggling with understanding Model, View, Service, Controller architecture and wrote some code.


Is my Model class the Model part of MVC (did I define it properly)?
Is my Service class a Service Layer (did I define the layer properly)?
Did I connect my layers properly (is the 'wiring' set up correctly)?


 /*
 * Application Idea:  Assume UI has two input boxes and a button and an output box
 * box A is for a number
 * box B is for a number
 * 
 * event of a button click sends responce of (A+B) into the output button
 * Implement Controller, Service, Model, View layers
 */
class Controller
{

    /**
     *
     * @var Service
     */
    private $service;

    /**
     *
     * @var Model
     */
    private $model;

    // add A B update C for all
    function addAB()
    {
        $a = $this-&gt;model-&gt;getA();
        $b = $this-&gt;model-&gt;getB();

        $c = $this-&gt;service-&gt;computeSum($a, $b);
        $this-&gt;model-&gt;setC($c);

        $view = new View();
        return $view-&gt;render($a, $b, $c);
    }

    public function getService()
    {
        return $this-&gt;service;
    }

    public function setService($service)
    {
        $this-&gt;service = $service;
    }

    public function getModel()
    {
        return $this-&gt;model;
    }

    public function setModel($model)
    {
        $this-&gt;model = $model;
    }
}

class ControllerFactory
{

    function getController(array $request)
    {
        $model = new Model();
        $model-&gt;setA($request['a']);
        $model-&gt;setB($request['b']);

        $service = new Service();

        $controller = new Controller();
        $controller-&gt;setService($service);
        $controller-&gt;setModel($model);

        return $controller;
    }
}

class Model
{

    private $a;

    private $b;

    private $c;

    public function getA()
    {
        return $this-&gt;a;
    }

    public function getB()
    {
        return $this-&gt;b;
    }

    public function getC()
    {
        return $this-&gt;c;
    }

    public function setA($a)
    {
        $this-&gt;a = $a;
    }

    public function setB($b)
    {
        $this-&gt;b = $b;
    }

    public function setC($c)
    {
        $this-&gt;c = $c;
    }
}

class Service
{

    function computeSum($a, $b)
    {
        return $a + $b;
    }
}

class View
{

    function render($a, $b, $c)
    {
        print "$a + $b = $c";
    }
}

class Router
{

    function routeRequest($request)
    {
        $controller = (new ControllerFactory())-&gt;getController($request);
        switch ($request['event'])
        {
            case 'add_event':
                $controller-&gt;addAB();
                break;
        }
    }
}

/*
 * request can come from Web Browser
 */
$request = array();
$request['event'] = "add_event";
$request['a'] = 2;
$request['b'] = 3;

$router = new Router();
$router-&gt;routeRequest($request);
Based on your example I would suggest you spend some more time on plain MVC before you try to work with a service layer. Something else to keep in mind is that the requirements of the application you're building determine whether you even need a service layer. In your example you don't. Actually since you're not really retrieving any data, your example doesn't need the model either.

The simplest way to describe the relationship between the controller and the view is that the controller does all the work and then sends data only to the view. The view then takes the data and displays it any which way it wants. This is valuable because it means you can make serious changes to one or the other or both without there being much risk of the changes causing undesired effect. It makes maintaining and expanding code much easier.

In your example you don't really make a clear separation between the view and the controller as far as the actual execution code goes. I would take a hint from MS.Net MVC and make better use of objects to separate the roles and interfaces of each layer properly.

Take this over-simplified example based on what you provided:

class View{}
class ViewData{}
class Controller{}
class ControllerData{}

$cData = new ControllerData();
$cData-&gt;event = "add_event";
$cData-&gt;a = 2;
$cData-&gt;b = 3;
$cObj = new Controller($cData);
$vData = $cObj-&gt;getData();   //returns a ViewData object
$vObj = new View($vData);
$vObj-&gt;renderView();