実用的なOOP:クイズアプリの構築-MVC
Feb 19, 2025 am 10:40 AM
- MVCアーキテクチャ:
- アプリケーションは、モデル-View-Controllerパターンを使用し、クイズと質問エンティティがモデルを形成し、ユーザーインターフェイスをビューとして形成し、ユーザーインタラクションフローを定義するクイズサービス(コントローラー)。 Slim Framework:
- データアクセス:データマッパーが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
startQuiz
getQuestion
スリムフレームワーク統合checkSolution
isOver
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 サイトの他の関連記事を参照してください。

人気の記事

人気の記事

ホットな記事タグ

メモ帳++7.3.1
使いやすく無料のコードエディター

SublimeText3 中国語版
中国語版、とても使いやすい

ゼンドスタジオ 13.0.1
強力な PHP 統合開発環境

ドリームウィーバー CS6
ビジュアル Web 開発ツール

SublimeText3 Mac版
神レベルのコード編集ソフト(SublimeText3)

ホットトピック











LaravelのバックエンドでReactアプリを構築する:パート2、React
