幸运的是我们曾在四月已把程序的核心逻辑代码与它的界面分离了。我将试着表达我们是如何做的,希望对你的工作有所帮助。
这个SourceForge的bugs跟踪器和其它的一些工具被分成两个库 - 这个HTML库和数据访问库。这个数据访问库检查输入的值的正确性,处理安全校验,并且当成功/失败时返回TRUE 或 FALSE。
由于简化的原因,这个例子并没有基于一个完善的对象模式,那样我还要解释这个基类和它的一些衍生类等等,我想这个例子将给你一个最普通的想法。
HTML 库的例子
//connect to database
require ("database.php");
//common utils like header/footer HTML
require ("html.php");
//data access library
require ("bug_data.php");
echo site_header("Page Title");
echo "
Updating A Bug
";
if (bug_data_update($field1,$field2,$field3)) {
echo "
Update Failed!
";
} else {
echo "
Updated Bug Successfully
";
//echo the global error string
echo $feedback;
}
echo site_footer();
?>
Data 访问库的例子
/**
*
* controls access to updating a bug in the
* database. Validates data and checks security
* Returns true on success, false on failure
*
*/
function bug_data_update ($field1,$field2,$field3) {
//global string to report back errors
global $feedback;
//$field1 and $field2 are required
if (!$field1 || !$field2) {
$feedback="Field 1 And Field 2 Are Required";
return false;
}
//make sure this user has permission to update
if (!user_isadmin()) {
$feedback="You Must Be An Admin To Update a Bug";
return fal
| 对此文章发表了评论 |
