2
\$\begingroup\$

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->model->getA();
        $b = $this->model->getB();

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

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

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

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

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

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

class ControllerFactory
{

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

        $service = new Service();

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

        return $controller;
    }
}

class Model
{

    private $a;

    private $b;

    private $c;

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

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

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

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

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

    public function setC($c)
    {
        $this->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())->getController($request);
        switch ($request['event'])
        {
            case 'add_event':
                $controller->addAB();
                break;
        }
    }
}

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

$router = new Router();
$router->routeRequest($request);
\$\endgroup\$
1
\$\begingroup\$

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->event = "add_event";
$cData->a = 2;
$cData->b = 3;
$cObj = new Controller($cData);
$vData = $cObj->getData();   //returns a ViewData object
$vObj = new View($vData);
$vObj->renderView();
| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.