What is the purpose of the controller in the MVC p

2019-01-20 07:10发布

问题:

I am just heading into MVC design pattern. A simple example here does not clear my concept about the use of controller. Could you please explain real use of controller while keeping it simple.

Model:

    class Model {
       public $text;

       public function __construct() {
           $this->text = 'Hello world!';
       }        
    }

Controller:

      class Controller {
          private $model;

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

View:

      class View {
         private $model;
         //private $controller;

         public function __construct(/*Controller $controller,*/ Model $model) {
             //$this->controller = $controller;
             $this->model = $model;
         }

          public function output() {
               return '<h1>' . $this->model->text .'</h1>';
         }

      }

index:

      require_once('Model.php'); 
      require_once('Controller.php');
      require_once('View.php');

      //initiate the triad
      $model = new Model();
      //It is important that the controller and the view share the model
      //$controller = new Controller($model);
      $view = new View(/*$controller,*/ $model);
      echo $view->output();

回答1:

Controllers purpose in MVC pattern is to alter the state of model layer.

It's done by controller receiving user input (preferable - abstracted as some thing like Request instance) and then, based on parameters that are extracted from the user input, passes the values to appropriate parts of model layer.

The communication with model layer usually happens via various services. These services in turn are responsible for governing the interaction between persistence abstraction and domain/business objects, or also called "application logic".

Example:

class Account extends \Components\Controller
{

    private $recognition;

    public function __construct(\Model\Services\Recognition $recognition)
    {
        $this->recognition = $recognition;
    }

    public function postLogin($request)
    {    
        $this->recognition->authenticate(
            $request->getParameter('credentials'),
            $request->getParameter('method')
        );
    }

    // other methods

}

What is controller NOT responsible for?

Controllers in MVC architectural pattern are NOT responsible for:

  • initializing parts of model layer
  • retrieving data from model layer
  • validation of input
  • initializing views
  • selecting templates
  • creating response
  • access control
  • .. etc.


回答2:

The controller in PHP ( or in fact in any server language, would be the one that handles the url that is called) So i will have to rewrite your above code it would be

Main file; this in fact will be your main controller or router, this is the first file that is called and usually would be called index.php ( in this file just create an instance of controller, no need to create model and view since they will be in purview of controller as to which model and view to use)

require_once('Controller.php');

//check the user url and decide which controller to instantiate. This is usually done by a separate file called controller factory. But for simplicity we use take this decision here itself.
$controller = new Controller()
//call controllers default method and let it decide which internal function to call
$controller->index()

Controller file => Here instantiate the model and view. The advantage is the model and view can be changed if required in the controller itself

require_once "Model.php"
require_once "View.php"
class Controller {
private $model;
private $view;

public function _contruct() {
// i may and may not initialize my mocel or view here, better initialize it in the function where you are going to use them.
}

public function index() {
// for now assume this controller just does one function that is to display the hello world. Otherwise we will read the called url and decide which further function to call
$model = new Model();
$view = new View($model); // pass the model to view
$view->output()  // don't ideally echo here, let view itself echo the content. This is helpfull  cases where you may have a view which echos the content or may want to format it in certain way (e.g JSON and return it to the caller
}

That's pretty much it . I dont see any code changes in your model or view files. Theu shoudl work as is except you will echo the content in view instead of returning it.