、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 サイトの他の関連記事を参照してください。