>本教程繼續使用自下而上的設計方法和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) },
,showAllQuizes
,startQuiz
,getQuestion
,checkSolution
,isOver
,getResult
,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();)在很大程度上保持不變,處理數據的表示。
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設置完成後,將HardCoded
mapper實例替換為index.php
> mapper。 Mongo
方法將數據庫行轉換為測驗和問題對象。 rowToEntity
結論和常見問題解答基本相同,強調了MVC模式,纖細框架和服務層設計的好處。 為了清楚起見,簡化了代碼示例。 完整的,準備生產的代碼將需要更全面的錯誤處理,輸入驗證和安全措施。
以上是實用的OOP:構建測驗應用-MVC的詳細內容。更多資訊請關注PHP中文網其他相關文章!

phpIdentifiesauser'ssessionSessionSessionCookiesAndSessionId.1)whiwsession_start()被稱為,phpgeneratesainiquesesesessionIdStoredInacookInAcookInAcienamedInAcienamedphpsessIdontheuser'sbrowser'sbrowser.2)thisIdallowSphptpptpptpptpptpptpptpptoretoreteretrieetrieetrieetrieetrieetrieetreetrieetrieetrieetrieetremthafromtheserver。

PHP會話的安全可以通過以下措施實現:1.使用session_regenerate_id()在用戶登錄或重要操作時重新生成會話ID。 2.通過HTTPS協議加密傳輸會話ID。 3.使用session_save_path()指定安全目錄存儲會話數據,並正確設置權限。

phpsessionFilesArestoredIntheDirectorySpecifiedBysession.save_path,通常是/tmponunix-likesystemsorc:\ windows \ windows \ temponwindows.tocustomizethis:tocustomizEthis:1)useession_save_save_save_path_path()

ToretrievedatafromaPHPsession,startthesessionwithsession_start()andaccessvariablesinthe$_SESSIONarray.Forexample:1)Startthesession:session_start().2)Retrievedata:$username=$_SESSION['username'];echo"Welcome,".$username;.Sessionsareserver-si

利用會話構建高效購物車系統的步驟包括:1)理解會話的定義與作用,會話是服務器端的存儲機制,用於跨請求維護用戶狀態;2)實現基本的會話管理,如添加商品到購物車;3)擴展到高級用法,支持商品數量管理和刪除;4)優化性能和安全性,通過持久化會話數據和使用安全的會話標識符。

本文討論了PHP中的crypt()和password_hash()的差異,以進行密碼哈希,重點介紹其實施,安全性和對現代Web應用程序的適用性。

文章討論了通過輸入驗證,輸出編碼以及使用OWASP ESAPI和HTML淨化器之類的工具來防止PHP中的跨站點腳本(XSS)。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

Atom編輯器mac版下載
最受歡迎的的開源編輯器

VSCode Windows 64位元 下載
微軟推出的免費、功能強大的一款IDE編輯器

WebStorm Mac版
好用的JavaScript開發工具

MantisBT
Mantis是一個易於部署的基於Web的缺陷追蹤工具,用於幫助產品缺陷追蹤。它需要PHP、MySQL和一個Web伺服器。請查看我們的演示和託管服務。

禪工作室 13.0.1
強大的PHP整合開發環境