2010-05-22 179 views
2

截至目前,我仍然對班級感到不安,所以我不想爲我的網站使用任何班級。我仍然在上課練習。如何在不使用類的情況下使用MVC創意?

但是我怎樣才能實現沒有類的MVC的想法?

這是否適用於MVC?

的index.php(視圖)

index_controller.php

index_model.php

Is this right for what a MVC should be? 
View: show html, css, forms 
Controller: get $_POST from forms and any data from the user, get info from db 
Model: do all the functions, insert/delete in db, etc 

基本上分離HTML/CSS的視圖,所有的數據收集爲控制器和模型的邏輯。只需使用require_once連接它們。

+3

是的,您可以做到這一點,請參閱Rasmus的最小MVC實現 - http://toys.lerdorf.com/archives/38-The-no-framework-PHP-MVC-framework.html。他爲DB和Model使用了類,但是可以將它們與函數交換。 *但**爲什麼**你不想使用類?* – Anurag 2010-05-22 03:23:05

+0

我還是新來的類,所以我不想在沒有先用小項目練習的情況下使用它。 :) – jpjp 2010-05-22 03:25:21

回答

1

控制器:您的index.php,接受和指示請求。這當然可以是一個'無階級'的腳本。它將充當控制器和「前端控制器」。

查看:演示文稿腳本的集合,您的控制器包含的特定腳本。從控制器的變量範圍實質上'獲取'數據。

模型:提供訪問您的數據的函數的集合。控制器確定要包含的請求。

當然,它可以可以被完成,但你鬆了很多沒有使用類(OOP)。以下是控制器可能與相似的快速示例。沒什麼了不起,只是一個想法。顯示控制器也應該對模型/視圖有所瞭解。

<?php 
    $action = getAction(); //parse the request to find the action 
    switch($action){ 
    case 'list': 
     include('models/todolist.php'); //include the model 
     $items = todolistGetItems(); //get the items using included function 
     include('views/todolist/list.php'); //include the view 
     break; 
    case 'add': 
     if(!empty($_POST['new'])){ 
     include('models/todolist.php'); //include the model 
     todolistAddItem($_POST); //add the item 
     $items = todolistGetItems(); //get the items using included function 
     include('views/todolist/list.php'); //include the view 
     } else { 
     include('views/todolist/add.php'); //include the view 
     } 
    } 
+0

添加了一個示例控制器。 – 2010-05-22 03:47:24

相關問題