如何在不使用类的情况下使用MVC创意?
问题描述:
截至目前,我仍然对班级感到不安,所以我不想为我的网站使用任何班级。我仍然在上课练习。如何在不使用类的情况下使用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连接它们。
答
控制器:您的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
是的,您可以做到这一点,请参阅Rasmus的最小MVC实现 - http://toys.lerdorf.com/archives/38-The-no-framework-PHP-MVC-framework.html。他为DB和Model使用了类,但是可以将它们与函数交换。 *但**为什么**你不想使用类?* – Anurag 2010-05-22 03:23:05
我还是新来的类,所以我不想在没有先用小项目练习的情况下使用它。 :) – jpjp 2010-05-22 03:25:21