2014-02-11 27 views
1

我试图理解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(); 

?> 
+2

确切位置在哪里,你发现*“半官方的MVC模式” *,因为它是废话。视图不应该有权访问控制器。 –

+1

http://r.je/mvc-in-php.html – SinisterGlitch

+0

http://www.sitepoint.com/the-mvc-pattern-and-php-1/ – SinisterGlitch

回答

0

首先,您对“半官方MVC模式”的定义是错误的。

下面是我能想到的最简单的“正确”MVC样板。

我知道,理解MVC原则可能有时很难,但不是写你自己的迷你框架,我会建议看看Laravel。这是一个记录丰富,功能丰富的现代框架,看起来难以学习,但相信我,在你掌握了基本知识之后,你就不会犯错。

<? 

/* 

Model represents a single entity. It doesn't have to be read or saved to the database. 
Example models: User, Post, Comment, Auction. 
Model class is used to operate on the model data, such as calculating the age of the user or calculating a comment count for a post. 
*/ 
class Model { 

    static function get($id) { 
     return $obj; // Returns an object with given ID from the DB 
    } 

    static function all() { 
     return $objc; // Returns all the objects. 
    } 

} 

class User extends Model { 
    public $name; 
    public $surname; 
} 

/* 

Controller handles the logic of the "app". It's used to load both the data and views and combine them together to serve output to the client. 
Please keep in mind, that sometimes a controller can be used without the need of a model object (static pages) or view object (json output) 

*/ 
class Controller { 

    public function index() { 
     $models = User::all();  
     $view = new View('index_view'); 
     $view->set('models', $models); 

     return $view->render(); 
    } 

    public function show($id) { 
     $model = User::get($id); 

     $view = new View('single_view'); 
     $view->set('model', $model); 

     return $view->render(); 
    } 

} 

/* 

View class is used to handle template loading and parsing. You can just include raw PHP files or create complex parsing rules 

*/ 
class View { 

    protected $file_name; 
    protected $fields; 

    public function __construct($filename) { 
     $this->file_name = $filename; 
    } 

    public function set($key, $value) { 
     $this->fields[$key] = $value; // Store variable in the fields array 
    } 

    public function render() { 
     extract($this->fields); // Make all the variables visible for the template file 
     include($this->file_name); // Include the template file 

     // You can add a buffer here to return the parsed template as a string. (see ob_flush() and ob_start()) 
    } 

} 

// single_view.php 
<h1> Hello <?= $model->name ?></h1> 

// index_view.php 
<table> 
    <? foreach($models as $model) { ?> 
     <tr> 
      <td><?= $model->name ?></td> 
      <td><?= $model->surname ?></td> 
    <? } ?> 
</table> 
+0

谢谢你这个例子的队友,如果我可以upvote你我会这样做(但我不能)。我见过10多个不同版本的MVC,这是最清晰的。我会实现你的风格,并与它一起玩,直到它变得直观。 – SinisterGlitch

+0

您能解释一下Laravel的MVC模式有什么问题吗? –

+0

一个好的开始将是“它不是MVC”。 –