>本教程继续使用自下而上的设计方法和MVC(模型视图控制器)模式构建测验应用程序。 第一部分涵盖了测验和问题实体的创建,占位符数据映射器以及服务界面。本部分着重于使用Slim Framework实施服务,控制器和视图,最后,用基于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中文网其他相关文章!