>本教程继续使用自下而上的设计方法和MVC(模型视图控制器)模式构建测验应用程序。 第一部分涵盖了测验和问题实体的创建,占位符数据映射器以及服务界面。本部分着重于使用Slim Framework实施服务,控制器和视图,最后,用基于MongoDB的一个。
密钥概念:
- MVC体系结构:该应用程序使用模型视图控制器模式,测验和问题实体形成模型,用户界面作为视图以及定义用户交互流的测验服务(控制器)。
- 纤细的框架: Slim为控制器和视图提供了框架。 它的模块化允许轻松替换其他MVC框架。
- 数据访问:
数据映射器连接到mongodb,抽象数据库交互。这允许轻松的数据库切换。 > 服务层和域模型: - 该应用程序采用服务层来封装业务逻辑,并遵守“脂肪模型,薄模型”的可维护性。 实施不可知论:
- 该服务旨在独立于用户界面,允许不同的前端(例如,命令行)。 服务实现(
QuizAppServiceQuiz
>核心服务类()在下面详细介绍。 请注意,会话变量用于简单;一个更强大的解决方案将使用专用的会话管理层来用于更广泛的应用程序上下文。
QuizAppServiceQuiz
,
<?php namespace QuizApp\Service; use QuizApp\Service\Quiz\Result; // ... class Quiz implements QuizInterface { // ... (constants remain the same) // ... (constructor remains the same) // ... (showAllQuizes remains the same) public function startQuiz($quizOrId) { // ... (logic remains largely the same) } // ... (getQuestion remains largely the same) public function checkSolution($solutionId) { // ... (logic remains largely the same) } // ... (isOver remains largely the same) // ... (getResult remains the same) // ... (getCurrentQuiz remains largely the same) // ... (getCurrentQuestionId remains the same) // ... (addResult remains the same) },
,showAllQuizes
,startQuiz
,getQuestion
,checkSolution
,isOver
,getResult
,getCurrentQuiz
getCurrentQuestionId
addResult
纤细的框架集成
Slim应用程序以初始化,设置路由和渲染。
视图(index.php
,
<?php require 'vendor/autoload.php'; session_start(); $service = new \QuizApp\Service\Quiz( new \QuizApp\Mapper\HardCoded() //Initially using HardCoded mapper ); $app = new \Slim\Slim(); $app->config(['templates.path' => './views']); // Routes (simplified for brevity) $app->get('/', function () use ($service, $app) { $app->render('choose-quiz.phtml', ['quizes' => $service->showAllQuizes()]); }); $app->get('/choose-quiz/:id', function ($id) use ($service, $app) { $service->startQuiz($id); $app->redirect('/solve-question'); }); $app->get('/solve-question', function () use ($service, $app) { $app->render('solve-question.phtml', ['question' => $service->getQuestion()]); }); $app->post('/check-answer', function () use ($service, $app) { $isCorrect = $service->checkSolution($app->request->post('id')); // ... (redirect logic remains the same) }); $app->get('/end', function () use ($service, $app) { $app->render('end.phtml', ['result' => $service->getResult()]); }); $app->run();)在很大程度上保持不变,处理数据的表示。
solve-question.phtml
end.phtml
QuizAppMapperMongo
映射器与mongoDB集合进行交互。 应添加错误处理和更强大的数据验证以进行生产。
<?php namespace QuizApp\Service; use QuizApp\Service\Quiz\Result; // ... class Quiz implements QuizInterface { // ... (constants remain the same) // ... (constructor remains the same) // ... (showAllQuizes remains the same) public function startQuiz($quizOrId) { // ... (logic remains largely the same) } // ... (getQuestion remains largely the same) public function checkSolution($solutionId) { // ... (logic remains largely the same) } // ... (isOver remains largely the same) // ... (getResult remains the same) // ... (getCurrentQuiz remains largely the same) // ... (getCurrentQuestionId remains the same) // ... (addResult remains the same) }
请记住,一旦完成MongoDB设置完成后,将HardCoded
mapper实例替换为index.php
> mapper。 Mongo
方法将数据库行转换为测验和问题对象。rowToEntity
结论和常见问题解答基本相同,强调了MVC模式,纤细框架和服务层设计的好处。 为了清楚起见,简化了代码示例。 完整的,准备生产的代码将需要更全面的错误处理,输入验证和安全措施。
以上是实用的OOP:构建测验应用-MVC的详细内容。更多信息请关注PHP中文网其他相关文章!

本文讨论了PHP中的crypt()和password_hash()之间的差异,以进行密码哈希,重点介绍其实施,安全性和对现代Web应用程序的适用性。

文章讨论了通过输入验证,输出编码以及使用OWASP ESAPI和HTML净化器之类的工具来防止PHP中的跨站点脚本(XSS)。

自动加载PHP会在需要时自动加载类文件,从而通过减少内存使用和增强代码组织来提高性能。最佳实践包括使用PSR-4和有效组织代码。

本文讨论了在PHP中管理文件上传大小的管理,重点是2MB的默认限制以及如何通过修改PHP.INI设置来增加它。

本文讨论了PHP 7.1中引入的PHP中的无效类型,允许变量或参数为指定类型或NULL。它突出显示了诸如提高可读性,类型安全性和明确意图的好处,并解释了如何声明

本文讨论了unset()和unlink()功能在编程中的差异,重点关注其目的和用例。 unset()从内存中删除变量,而unlink()从文件系统中删除文件。两者都对效率至关重要


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

ZendStudio 13.5.1 Mac
功能强大的PHP集成开发环境

螳螂BT
Mantis是一个易于部署的基于Web的缺陷跟踪工具,用于帮助产品缺陷跟踪。它需要PHP、MySQL和一个Web服务器。请查看我们的演示和托管服务。

SecLists
SecLists是最终安全测试人员的伙伴。它是一个包含各种类型列表的集合,这些列表在安全评估过程中经常使用,都在一个地方。SecLists通过方便地提供安全测试人员可能需要的所有列表,帮助提高安全测试的效率和生产力。列表类型包括用户名、密码、URL、模糊测试有效载荷、敏感数据模式、Web shell等等。测试人员只需将此存储库拉到新的测试机上,他就可以访问到所需的每种类型的列表。

记事本++7.3.1
好用且免费的代码编辑器

DVWA
Damn Vulnerable Web App (DVWA) 是一个PHP/MySQL的Web应用程序,非常容易受到攻击。它的主要目标是成为安全专业人员在合法环境中测试自己的技能和工具的辅助工具,帮助Web开发人员更好地理解保护Web应用程序的过程,并帮助教师/学生在课堂环境中教授/学习Web应用程序安全。DVWA的目标是通过简单直接的界面练习一些最常见的Web漏洞,难度各不相同。请注意,该软件中