<abbr id="40or5"><listing id="40or5"></listing></abbr>
  • <abbr id="40or5"><listing id="40or5"></listing></abbr>\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

    定義到控制器的路由對(duì)映。
    \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)用程式的入口點(diǎn)。
    \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)點(diǎn)和缺點(diǎn)\n<\/h2>\n\n

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

      \n
    • \n組織<\/strong>:控制器是模組化的,每個(gè)控制器處理一個(gè)特定的頁面。 <\/li>\n
    • \n可重複使用性<\/strong>:視圖可以在不同的控制器之間重複使用。 <\/li>\n
    • \n偵錯(cuò)<\/strong>:由於每個(gè)頁面都有自己的專用控制器,因此更容易追蹤錯(cuò)誤。 <\/li>\n<\/ul>\n\n

      缺點(diǎn)<\/strong><\/p>\n\n

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

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

          對(duì)於更複雜的項(xiàng)目,存在大量邏輯重用或多個(gè)入口點(diǎn),前端控制器<\/strong>或完整MVC架構(gòu)<\/strong>等模式可能更合適。 <\/p>\n\n\n \n\n \n \n\n \n "}

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

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

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

          Dec 28, 2024 am 07:42 AM

          PHP Design Patterns: Page Controller

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

          什麼是頁面控制器?

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

          1. 處理請(qǐng)求:處理客戶端發(fā)送的資料。
          2. 執(zhí)行頁面特定邏輯:驗(yàn)證輸入、與模型互動(dòng)或執(zhí)行計(jì)算。
          3. 渲染回應(yīng):將處理後的資料傳遞到視圖(範(fàn)本)並將最終回應(yīng)傳回給客戶端。

          該模式的優(yōu)點(diǎn)

          1. 簡(jiǎn)單流程:每個(gè)頁面都對(duì)應(yīng)到自己的專用控制器。
          2. 關(guān)注點(diǎn)分離:每個(gè)控制器只處理自己的邏輯。
          3. 可維護(hù)性:對(duì)一個(gè)頁面的變更僅影響其關(guān)聯(lián)的控制器。
          4. 可擴(kuò)充性:新增頁面非常簡(jiǎn)單,且不會(huì)破壞現(xiàn)有功能。

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

          典型的實(shí)作涉及以下元件:

          • 控制器:包含特定頁面邏輯的PHP檔案。
          • 路由:將 URL 對(duì)應(yīng)到控制器的路由機(jī)制。
          • 視圖:用於渲染使用者介面的範(fàn)本。

          流動(dòng)

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

          實(shí)施例

          檔案結(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
          

          自動(dòng)載入器

          {
              "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

          定義到控制器的路由對(duì)映。

          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)用程式的入口點(diǎn)。

          /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)點(diǎn)和缺點(diǎn)

          優(yōu)點(diǎn)

          • 組織:控制器是模組化的,每個(gè)控制器處理一個(gè)特定的頁面。
          • 可重複使用性:視圖可以在不同的控制器之間重複使用。
          • 偵錯(cuò):由於每個(gè)頁面都有自己的專用控制器,因此更容易追蹤錯(cuò)誤。

          缺點(diǎn)

          • 控制器數(shù)量增加:大型專案可能導(dǎo)致控制器激增,需要更好的組織。
          • 程式碼重複:控制器之間的通用邏輯可能會(huì)重複。這可以透過使用基本控制器類別來緩解。

          何時(shí)使用頁面控制器模式?

          • 簡(jiǎn)單系統(tǒng):最適合每個(gè)頁面都有特定邏輯的中小型 Web 應(yīng)用程式。
          • 模組化項(xiàng)目:當(dāng)您想要隔離邏輯以便於維護(hù)。
          • 沒有框架:非常適合沒有強(qiáng)大框架(如 Laravel 或 Symfony)的 PHP 專案。

          對(duì)於更複雜的項(xiàng)目,存在大量邏輯重用或多個(gè)入口點(diǎn),前端控制器或完整MVC架構(gòu)等模式可能更合適。

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

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

          熱AI工具

          Undress AI Tool

          Undress AI Tool

          免費(fèi)脫衣圖片

          Undresser.AI Undress

          Undresser.AI Undress

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

          AI Clothes Remover

          AI Clothes Remover

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

          Clothoff.io

          Clothoff.io

          AI脫衣器

          Video Face Swap

          Video Face Swap

          使用我們完全免費(fèi)的人工智慧換臉工具,輕鬆在任何影片中換臉!

          熱工具

          記事本++7.3.1

          記事本++7.3.1

          好用且免費(fèi)的程式碼編輯器

          SublimeText3漢化版

          SublimeText3漢化版

          中文版,非常好用

          禪工作室 13.0.1

          禪工作室 13.0.1

          強(qiáng)大的PHP整合開發(fā)環(huán)境

          Dreamweaver CS6

          Dreamweaver CS6

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

          SublimeText3 Mac版

          SublimeText3 Mac版

          神級(jí)程式碼編輯軟體(SublimeText3)

          如何在PHP中實(shí)施身份驗(yàn)證和授權(quán)? 如何在PHP中實(shí)施身份驗(yàn)證和授權(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中的文件上傳,核心在於驗(yàn)證文件類型、重命名文件並限制權(quán)限。 1.使用finfo_file()檢查真實(shí)MIME類型,僅允許特定類型如image/jpeg;2.用uniqid()生成隨機(jī)文件名,存儲(chǔ)至非Web根目錄;3.通過php.ini和HTML表單限製文件大小,設(shè)置目錄權(quán)限為0755;4.使用ClamAV掃描惡意軟件,增強(qiáng)安全性。這些步驟有效防止安全漏洞,確保文件上傳過程安全可靠。

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

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

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

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

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

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

          我如何了解最新的PHP開發(fā)和最佳實(shí)踐? 我如何了解最新的PHP開發(fā)和最佳實(shí)踐? 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時(shí)區(qū)? 如何設(shè)置PHP時(shí)區(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