• \n

    <\/h1>\n

    <\/p>\n<\/body>\n<\/html>\n<\/pre>\n\n\n\n

    ViewRenderer<\/strong>
    \n<\/p>\n\n

    namespace App\\Services;\n\nclass ViewRenderer {\n\n    public function render(string $view, array $data = []): void {\n        extract($data); \/\/ Turns array keys into variables\n        include __DIR__ . \"\/..\/..\/Views\/{$view}.html.php\";\n    }\n}\n<\/pre>\n\n\n\n

    HomeController<\/strong><\/p>\n\n

    處理主頁邏輯。
    \n<\/p>\n\n

    namespace App\\Controllers;\n\nuse App\\Services\\ViewRenderer;\n\nclass HomeController {\n\n    public function __construct(private ViewRenderer $viewRenderer)\n    {\n    }\n\n    public function handleRequest(): void {\n        $data = [\n            'title' => 'Welcome to the Site',\n            'content' => 'Homepage content.',\n        ];\n\n        $this->viewRenderer->render('home', $data);\n    }\n}\n<\/pre>\n\n\n\n

    關(guān)于控制器<\/strong><\/p>\n\n

    處理“關(guān)于我們”頁面邏輯。
    \n<\/p>\n\n

    namespace App\\Controllers;\n\nuse App\\Services\\ViewRenderer;\n\nclass AboutController\n{\n\n    public function __construct(private ViewRenderer $viewRenderer)\n    {\n    }\n\n    public function handleRequest(): void {\n        $data = [\n            'title' => 'About Us',\n            'content' => 'Information about the company.',\n        ];\n\n        $this->viewRenderer->render('about', $data);\n    }\n}\n<\/pre>\n\n\n\n

    routes.php<\/strong><\/p>\n\n

    定義到控制器的路由映射。
    \n<\/p>\n\n

    use App\\Controllers\\HomeController;\nuse App\\Controllers\\AboutController;\n\n\/\/ Define the routes in an associative array\nreturn [\n    '\/' => HomeController::class,\n    '\/about' => AboutController::class,\n];\n<\/pre>\n\n\n\n

    index.php<\/strong><\/p>\n

    應(yīng)用程序的入口點。
    \n<\/p>\n\n

    \/htdocs\n    \/src\n        \/Controllers\n            HomeController.php\n            AboutController.php\n        \/Services\n            ViewRenderer.php\n        \/Views\n            home.html.php\n            about.html.php\n    \/public\n        index.php\n    \/routes.php\n    composer.json\n<\/pre>\n\n\n\n

    \n \n \n 優(yōu)點和缺點\n<\/h2>\n\n

    優(yōu)點<\/strong><\/p>\n\n

      \n
    • \n組織<\/strong>:控制器是模塊化的,每個控制器處理一個特定的頁面。<\/li>\n
    • \n可重用性<\/strong>:視圖可以在不同的控制器之間重用。<\/li>\n
    • \n調(diào)試<\/strong>:由于每個頁面都有自己的專用控制器,因此更容易跟蹤錯誤。<\/li>\n<\/ul>\n\n

      缺點<\/strong><\/p>\n\n

        \n
      • \n控制器數(shù)量增加<\/strong>:大型項目可能導(dǎo)致控制器激增,需要更好的組織。<\/li>\n
      • \n代碼重復(fù)<\/strong>:控制器之間的通用邏輯可能會重復(fù)。這可以通過使用基本控制器類來緩解。<\/li>\n<\/ul>\n\n

        \n \n \n 何時使用頁面控制器模式?\n<\/h2>\n\n
          \n
        • \n簡單系統(tǒng)<\/strong>:最適合每個頁面都有特定邏輯的中小型 Web 應(yīng)用程序。<\/li>\n
        • \n模塊化項目<\/strong>:當(dāng)您想要隔離邏輯以便于維護時。<\/li>\n
        • \n沒有框架<\/strong>:非常適合沒有強大框架(如 Laravel 或 Symfony)的 PHP 項目。<\/li>\n<\/ul>\n\n

          對于更復(fù)雜的項目,存在大量邏輯重用或多個入口點,前端控制器<\/strong>或完整MVC架構(gòu)<\/strong>等模式可能更合適。<\/p>\n\n\n \n\n \n \n\n \n "}

          国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

          首頁 后端開發(fā) php教程 PHP 設(shè)計模式:頁面控制器

          PHP 設(shè)計模式:頁面控制器

          Dec 28, 2024 am 07:42 AM

          PHP Design Patterns: Page Controller

          頁面控制器設(shè)計模式是基于 Web 的系統(tǒng)中使用的常見架構(gòu)方法。它通過專用特定控制器來處理單個頁面或請求的邏輯來組織控制流。這種方法有助于隔離職責(zé),使代碼庫更易于維護和發(fā)展。

          什么是頁面控制器?

          頁面控制器模式中,每個頁面(或一組具有類似行為的頁面)都有自己的控制器,負責(zé):

          1. 處理請求:處理客戶端發(fā)送的數(shù)據(jù)。
          2. 執(zhí)行頁面特定邏輯:驗證輸入、與模型交互或執(zhí)行計算。
          3. 渲染響應(yīng):將處理后的數(shù)據(jù)傳遞到視圖(模板)并將最終響應(yīng)返回給客戶端。

          該模式的優(yōu)點

          1. 簡單流程:每個頁面都映射到自己的專用控制器。
          2. 關(guān)注點分離:每個控制器只處理自己的邏輯。
          3. 可維護性:對一個頁面的更改僅影響其關(guān)聯(lián)的控制器。
          4. 可擴展性:添加新頁面非常簡單,并且不會破壞現(xiàn)有功能。

          基本結(jié)構(gòu)

          典型的實現(xiàn)涉及以下組件:

          • 控制器:包含特定頁面邏輯的PHP文件。
          • 路由:將 URL 映射到控制器的路由機制。
          • 視圖:用于渲染用戶界面的模板。

          流動

          1. 客戶端向特定 URL 發(fā)送請求。
          2. 路由系統(tǒng)為請求識別適當(dāng)?shù)目刂破鳌?/li>
          3. 控制器執(zhí)行所需的邏輯并將響應(yīng)渲染委托給視圖。
          4. 視圖生成最終輸出并將其返回給客戶端。

          實施例

          文件結(jié)構(gòu)

          /htdocs
              /src
                  /Controllers
                      HomeController.php
                      AboutController.php
                  /Services
                      ViewRenderer.php
                  /Views
                      home.html.php
                      about.html.php
              /public
                  index.php
              /routes.php
              composer.json
          

          自動加載器

          {
              "autoload": {
                  "psr-4": {
                      "App\": "htdocs/"
                  }
              }
          }
          
          composer dump-autoload
          

          模板

          主頁about.html.php.
          的模板

          <!DOCTYPE html>
          <html>
          <head>
              <title><?= htmlspecialchars($title) ?></title>
          </head>
          <body>
              <h1><?= htmlspecialchars($title) ?></h1>
              <p><?= htmlspecialchars($content) ?></p>
          </body>
          </html>
          

          ViewRenderer

          namespace App\Services;
          
          class ViewRenderer {
          
              public function render(string $view, array $data = []): void {
                  extract($data); // Turns array keys into variables
                  include __DIR__ . "/../../Views/{$view}.html.php";
              }
          }
          

          HomeController

          處理主頁邏輯。

          namespace App\Controllers;
          
          use App\Services\ViewRenderer;
          
          class HomeController {
          
              public function __construct(private ViewRenderer $viewRenderer)
              {
              }
          
              public function handleRequest(): void {
                  $data = [
                      'title' => 'Welcome to the Site',
                      'content' => 'Homepage content.',
                  ];
          
                  $this->viewRenderer->render('home', $data);
              }
          }
          

          關(guān)于控制器

          處理“關(guān)于我們”頁面邏輯。

          namespace App\Controllers;
          
          use App\Services\ViewRenderer;
          
          class AboutController
          {
          
              public function __construct(private ViewRenderer $viewRenderer)
              {
              }
          
              public function handleRequest(): void {
                  $data = [
                      'title' => 'About Us',
                      'content' => 'Information about the company.',
                  ];
          
                  $this->viewRenderer->render('about', $data);
              }
          }
          

          routes.php

          定義到控制器的路由映射。

          use App\Controllers\HomeController;
          use App\Controllers\AboutController;
          
          // Define the routes in an associative array
          return [
              '/' => HomeController::class,
              '/about' => AboutController::class,
          ];
          

          index.php

          應(yīng)用程序的入口點。

          /htdocs
              /src
                  /Controllers
                      HomeController.php
                      AboutController.php
                  /Services
                      ViewRenderer.php
                  /Views
                      home.html.php
                      about.html.php
              /public
                  index.php
              /routes.php
              composer.json
          

          優(yōu)點和缺點

          優(yōu)點

          • 組織:控制器是模塊化的,每個控制器處理一個特定的頁面。
          • 可重用性:視圖可以在不同的控制器之間重用。
          • 調(diào)試:由于每個頁面都有自己的專用控制器,因此更容易跟蹤錯誤。

          缺點

          • 控制器數(shù)量增加:大型項目可能導(dǎo)致控制器激增,需要更好的組織。
          • 代碼重復(fù):控制器之間的通用邏輯可能會重復(fù)。這可以通過使用基本控制器類來緩解。

          何時使用頁面控制器模式?

          • 簡單系統(tǒng):最適合每個頁面都有特定邏輯的中小型 Web 應(yīng)用程序。
          • 模塊化項目:當(dāng)您想要隔離邏輯以便于維護時。
          • 沒有框架:非常適合沒有強大框架(如 Laravel 或 Symfony)的 PHP 項目。

          對于更復(fù)雜的項目,存在大量邏輯重用或多個入口點,前端控制器或完整MVC架構(gòu)等模式可能更合適。

          以上是PHP 設(shè)計模式:頁面控制器的詳細內(nèi)容。更多信息請關(guān)注PHP中文網(wǎng)其他相關(guān)文章!

          本站聲明
          本文內(nèi)容由網(wǎng)友自發(fā)貢獻,版權(quán)歸原作者所有,本站不承擔(dān)相應(yīng)法律責(zé)任。如您發(fā)現(xiàn)有涉嫌抄襲侵權(quán)的內(nèi)容,請聯(lián)系admin@php.cn

          熱AI工具

          Undress AI Tool

          Undress AI Tool

          免費脫衣服圖片

          Undresser.AI Undress

          Undresser.AI Undress

          人工智能驅(qū)動的應(yīng)用程序,用于創(chuàng)建逼真的裸體照片

          AI Clothes Remover

          AI Clothes Remover

          用于從照片中去除衣服的在線人工智能工具。

          Clothoff.io

          Clothoff.io

          AI脫衣機

          Video Face Swap

          Video Face Swap

          使用我們完全免費的人工智能換臉工具輕松在任何視頻中換臉!

          熱工具

          記事本++7.3.1

          記事本++7.3.1

          好用且免費的代碼編輯器

          SublimeText3漢化版

          SublimeText3漢化版

          中文版,非常好用

          禪工作室 13.0.1

          禪工作室 13.0.1

          功能強大的PHP集成開發(fā)環(huán)境

          Dreamweaver CS6

          Dreamweaver CS6

          視覺化網(wǎng)頁開發(fā)工具

          SublimeText3 Mac版

          SublimeText3 Mac版

          神級代碼編輯軟件(SublimeText3)

          如何在PHP中實施身份驗證和授權(quán)? 如何在PHP中實施身份驗證和授權(quán)? Jun 20, 2025 am 01:03 AM

          tosecurelyhandleauthenticationandationallizationInphp,lofterTheSesteps:1.AlwaysHashPasswordSwithPassword_hash()andverifyusingspasspassword_verify(),usepreparedStatatementStopreventsqlineptions,andStoreSeruserDatain usseruserDatain $ _sessiveferterlogin.2.implementrole-2.imaccessccsccccccccccccccccccccccccc.

          如何在PHP中安全地處理文件上傳? 如何在PHP中安全地處理文件上傳? Jun 19, 2025 am 01:05 AM

          要安全處理PHP中的文件上傳,核心在于驗證文件類型、重命名文件并限制權(quán)限。1.使用finfo_file()檢查真實MIME類型,僅允許特定類型如image/jpeg;2.用uniqid()生成隨機文件名,存儲至非Web根目錄;3.通過php.ini和HTML表單限制文件大小,設(shè)置目錄權(quán)限為0755;4.使用ClamAV掃描惡意軟件,增強安全性。這些步驟有效防止安全漏洞,確保文件上傳過程安全可靠。

          PHP中==(松散比較)和===(嚴(yán)格的比較)之間有什么區(qū)別? PHP中==(松散比較)和===(嚴(yán)格的比較)之間有什么區(qū)別? Jun 19, 2025 am 01:07 AM

          在PHP中,==與===的主要區(qū)別在于類型檢查的嚴(yán)格程度。==在比較前會進行類型轉(zhuǎn)換,例如5=="5"返回true,而===要求值和類型都相同才會返回true,例如5==="5"返回false。使用場景上,===更安全應(yīng)優(yōu)先使用,==僅在需要類型轉(zhuǎn)換時使用。

          如何在PHP( - , *, /,%)中執(zhí)行算術(shù)操作? 如何在PHP( - , *, /,%)中執(zhí)行算術(shù)操作? Jun 19, 2025 pm 05:13 PM

          PHP中使用基本數(shù)學(xué)運算的方法如下:1.加法用 號,支持整數(shù)和浮點數(shù),也可用于變量,字符串?dāng)?shù)字會自動轉(zhuǎn)換但不推薦依賴;2.減法用-號,變量同理,類型轉(zhuǎn)換同樣適用;3.乘法用*號,適用于數(shù)字及類似字符串;4.除法用/號,需避免除以零,并注意結(jié)果可能是浮點數(shù);5.取模用%號,可用于判斷奇偶數(shù),處理負數(shù)時余數(shù)符號與被除數(shù)一致。正確使用這些運算符的關(guān)鍵在于確保數(shù)據(jù)類型清晰并處理好邊界情況。

          如何與PHP的NOSQL數(shù)據(jù)庫(例如MongoDB,Redis)進行交互? 如何與PHP的NOSQL數(shù)據(jù)庫(例如MongoDB,Redis)進行交互? Jun 19, 2025 am 01:07 AM

          是的,PHP可以通過特定擴展或庫與MongoDB和Redis等NoSQL數(shù)據(jù)庫交互。首先,使用MongoDBPHP驅(qū)動(通過PECL或Composer安裝)創(chuàng)建客戶端實例并操作數(shù)據(jù)庫及集合,支持插入、查詢、聚合等操作;其次,使用Predis庫或phpredis擴展連接Redis,執(zhí)行鍵值設(shè)置與獲取,推薦phpredis用于高性能場景,Predis則便于快速部署;兩者均適用于生產(chǎn)環(huán)境且文檔完善。

          我如何了解最新的PHP開發(fā)和最佳實踐? 我如何了解最新的PHP開發(fā)和最佳實踐? Jun 23, 2025 am 12:56 AM

          TostaycurrentwithPHPdevelopmentsandbestpractices,followkeynewssourceslikePHP.netandPHPWeekly,engagewithcommunitiesonforumsandconferences,keeptoolingupdatedandgraduallyadoptnewfeatures,andreadorcontributetoopensourceprojects.First,followreliablesource

          什么是PHP,為什么它用于Web開發(fā)? 什么是PHP,為什么它用于Web開發(fā)? Jun 23, 2025 am 12:55 AM

          PHPbecamepopularforwebdevelopmentduetoitseaseoflearning,seamlessintegrationwithHTML,widespreadhostingsupport,andalargeecosystemincludingframeworkslikeLaravelandCMSplatformslikeWordPress.Itexcelsinhandlingformsubmissions,managingusersessions,interacti

          如何設(shè)置PHP時區(qū)? 如何設(shè)置PHP時區(qū)? Jun 25, 2025 am 01:00 AM

          tosetTherightTimeZoneInphp,restate_default_timezone_set()functionAtthestArtofyourscriptWithavalIdidentIdentifiersuchas'america/new_york'.1.usedate_default_default_timezone_set_set()

          See all articles
        • <noframes id="aqk2c"><code id="aqk2c"></code></noframes>
          <fieldset id="aqk2c"><dd id="aqk2c"></dd></fieldset>
          <option id="aqk2c"></option>
          • <fieldset id="aqk2c"><acronym id="aqk2c"></acronym></fieldset>