2014-02-11 30 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」。 –