【发布时间】:2014-06-22 22:17:48
【问题描述】:
我已经看过了:
据此,我将有以下基本代码:
//Instance of a Model
$model = new Model();
//Controller and View get the Model
$controller = new Controller($model);
$view = new View($model);
//Controller change/work with the Model
$controller->doSomeAction();
//Display the final Model
$view->display();
我已经在领域驱动设计中实现了我的应用程序。但是现在我被困在了预置层,我想在经典 MVC 中实现它。
目前,我的控制器会创建模型和视图的实例(上面的代码似乎有问题):
//Get Model
$model = $myRepository->findById(42);
//Do Some stuff
$model->foo = 'foo';
$model->bar = 'bar';
//View
$view = new MyView($model)
$view->render();
Id 42 来自请求。但是我如何根据第一个真正的 MVC 代码来传输它呢?我的意思是,我没有静态模型,模型是由请求动态的。
这样的事情感觉不对,因为模型知道请求:
class MyPresentationModel extends PresentationModel {
public $foo;
public $bar;
public function __construct($request) {
//init myRepo...
$obj = $myRepo->findById($request->get(42));
$this->foo = $obj->getFoo();
$this->bar= $obj->getBar();
}
}
那么填充我的演示模型的最佳做法是什么?
【问题讨论】:
标签: php design-patterns model-view-controller model presentation