首页 > 后端开发 > php教程 > 实用的OOP:构建测验应用-MVC

实用的OOP:构建测验应用-MVC

Jennifer Aniston
发布: 2025-02-19 10:40:09
原创
721 人浏览过

Practical OOP: Building a Quiz App - MVC

>本教程继续使用自下而上的设计方法和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)
}
登录后复制
登录后复制

showAllQuizesstartQuizgetQuestioncheckSolutionisOvergetResult方法的代码在很大程度上保持不变,与原件保持不变。专注于核心功能。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();
登录后复制
)在很大程度上保持不变,处理数据的表示。

choose-quiz.phtml)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设置完成后,将HardCodedmapper实例替换为index.php> mapper。 Mongo方法将数据库行转换为测验和问题对象。> rowToEntity结论和常见问题解答基本相同,强调了MVC模式,纤细框架和服务层设计的好处。 为了清楚起见,简化了代码示例。 完整的,准备生产的代码将需要更全面的错误处理,输入验证和安全措施。

以上是实用的OOP:构建测验应用-MVC的详细内容。更多信息请关注PHP中文网其他相关文章!

本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
作者最新文章
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板