Konsep Utama:
Kelas Perkhidmatan Teras () terperinci di bawah. Perhatikan bahawa pembolehubah sesi digunakan untuk kesederhanaan; Penyelesaian yang lebih mantap akan menggunakan lapisan pengurusan sesi khusus untuk konteks aplikasi yang lebih luas. 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
, dan checkSolution
kaedah tetap tidak berubah dari asal, memberi tumpuan kepada fungsi teras. isOver
getResult
getCurrentQuiz
Integrasi kerangka Slim getCurrentQuestionId
addResult
, menyediakan penghalaan dan rendering.
Pandangan (, 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
MongoDB mapper (solve-question.phtml
end.phtml
<?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) }
ingat untuk menggantikan contoh HardCoded
mapper dalam index.php
dengan pemetaan Mongo
sebaik sahaja persediaan MongoDB selesai. Kaedah rowToEntity
mengendalikan penukaran baris pangkalan data ke dalam kuiz dan objek soalan.
Kesimpulan dan Soalan Lazim tetap sama, menekankan manfaat corak MVC, rangka kerja tipis, dan reka bentuk lapisan perkhidmatan. Contoh kod dipermudahkan untuk kejelasan. Lengkap, kod siap sedia akan memerlukan pengendalian ralat yang lebih komprehensif, pengesahan input, dan langkah keselamatan.
Atas ialah kandungan terperinci OOP Praktikal: Membina Aplikasi Kuiz - MVC. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!