我试图理解MVC模式,我有一些问题。 这是一个可以工作的例子,它是一个半正式的MVC模式。原理MVC理解为什么
class model {
public $string;
public function __construct(){
$this->string = 'awsome MVC';
}
}
class view {
private $model;
private $controller;
public function __construct($controller, $model){
$this->controller = $controller;
$this->model = $model;
}
public function output(){
return '<b>'.$this->model->string.'</b>';
}
}
class controller {
private $model;
public function __construct($model){
$this->model = $model;
}
}
$model = new Model();
$controller = new controller($model);
$view = new view($controller, $model);
echo $view->output();
我有这个问题,为什么视图处理更多然后控制器? 下面我有我自己的(我认为)MVC脚本,我希望你们可以指出我做错了什么,以及为什么上面的例子更好。
<?php
/*
* this is the view
*/
class template
{
function html($input){
return 'template : <b>'.$input.'</b>';
}
}
/*
* this is the model
*/
class database
{
function output(){
return 'this is a news title';
}
}
/*
* this is the basic controller
*/
class basicController
{
public $model;
public $view;
public function loadModel($model){
$this->model = new $model;
}
public function loadView($view){
$this->view = new $view;
}
}
/*
* this is the extended controller
*/
class newsController extends basicController
{
public function __construct(){
$this->loadModel('database');
$this->loadView('template');
}
public function showAction(){
return $this->view->html($this->model->output());
}
}
/*
* calling
*/
$controller = new newsController;
echo $controller->showAction();
?>
确切位置在哪里,你发现*“半官方的MVC模式” *,因为它是废话。视图不应该有权访问控制器。 –
http://r.je/mvc-in-php.html – SinisterGlitch
http://www.sitepoint.com/the-mvc-pattern-and-php-1/ – SinisterGlitch