ホームページ > バックエンド開発 > PHPチュートリアル > 実用的なOOP:クイズアプリの構築-MVC

実用的なOOP:クイズアプリの構築-MVC

Jennifer Aniston
リリース: 2025-02-19 10:40:09
オリジナル
720 人が閲覧しました

Practical OOP: Building a Quiz App - MVC

このチュートリアルは、ボトムアップデザインアプローチとMVC(Model-View-Controller)パターンを使用して、クイズアプリケーションの構築を続けています。 パート1は、クイズと質問エンティティの作成、プレースホルダーデータマッパー、およびサービスインターフェイスをカバーしました。このパートは、スリムフレームワークを使用してサービス、コントローラー、およびビューの実装に焦点を当て、最後に、ダミーマッパーをMongoDBベースのものに置き換えることに焦点を当てています。

重要な概念:

    MVCアーキテクチャ:
  • アプリケーションは、モデル-View-Controllerパターンを使用し、クイズと質問エンティティがモデルを形成し、ユーザーインターフェイスをビューとして形成し、ユーザーインタラクションフローを定義するクイズサービス(コントローラー)。
  • Slim Framework: Slimは、コントローラーとビューのフレームワークを提供します。 そのモジュール性により、他のMVCフレームワークに簡単に交換できるようになります。
  • データアクセス:データマッパーがMongoDBに接続し、データベースの相互作用を抽象化します。これにより、データベースの切り替えが簡単になります
  • サービスレイヤーおよびドメインモデル:アプリケーションはサービスレイヤーを使用してビジネスロジックをカプセル化し、保守性のための「脂肪モデル、薄いコントローラー」原理を順守しています。
  • 実装Agnostismision:サービスは、ユーザーインターフェイスから独立しているように設計されており、さまざまなフロントエンド(例:コマンドライン)を可能にします。
  • サービス実装(
  • コアサービスクラス(
  • )を以下に詳しく説明します。 セッション変数は単純さのために使用されることに注意してください。より堅牢なソリューションは、より広いアプリケーションコンテキストに専用のセッション管理レイヤーを使用します。

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 startQuizgetQuestionスリムフレームワーク統合checkSolutionisOver getResultスリムアプリケーションはgetCurrentQuizで初期化され、ルーティングとレンダリングをセットアップします。 getCurrentQuestionId addResultビュー(

)は、ほぼ同じままで、データのプレゼンテーションを処理します。

mongodb mapper(

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();
ログイン後にコピー

マッパーは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)
}
ログイン後にコピー
ログイン後にコピー

HardCodedマッパーインスタンスをindex.phpのマッパーに置き換えることを忘れないでください。MongoDBセットアップが完了したら。 Mongoメソッドは、データベースの行のクイズと質問オブジェクトへの変換を処理します。 rowToEntity結論とFAQはほぼ同じままであり、MVCパターン、スリムフレームワーク、およびサービスレイヤー設計の利点を強調しています。 コードの例は、明確にするために簡素化されます。 完全な生産準備コードでは、より包括的なエラー処理、入力検証、セキュリティ対策が必要です。

以上が実用的なOOP:クイズアプリの構築-MVCの詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
著者別の最新記事
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート