<rp id="yrjzu"><input id="yrjzu"></input></rp>
<menu id="yrjzu"></menu>
<i id="yrjzu"><input id="yrjzu"><div id="yrjzu"></div></input></i>

<p id="yrjzu"><strike id="yrjzu"></strike></p><track id="yrjzu"><input id="yrjzu"></input></track>
<span id="yrjzu"><video id="yrjzu"></video></span>
    <track id="yrjzu"><th id="yrjzu"></th></track>
\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

關於控制器<\/strong><\/p>\n\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

應用程式的入口點。
\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偵錯<\/strong>:由於每個頁面都有自己的專用控制器,因此更容易追蹤錯誤。 <\/li>\n<\/ul>\n\n

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

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

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

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

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

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

        PHP 設計模式:頁面控制器

        Dec 28, 2024 am 07:42 AM

        PHP Design Patterns: Page Controller

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

        什麼是頁面控制器?

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

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

        該模式的優(yōu)點

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

        基本結構

        典型的實作涉及以下元件:

        • 控制器:包含特定頁面邏輯的PHP檔案。
        • 路由:將 URL 對應到控制器的路由機制。
        • 視圖:用於渲染使用者介面的範本。

        流動

        1. 客戶端向特定 URL 發(fā)送請求。
        2. 路由系統(tǒng)為請求識別適當?shù)目刂破鳌?
        3. 控制器執(zhí)行所需的邏輯並將回應渲染委託給視圖。
        4. 視圖產(chǎ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
        

        自動載入器

        {
            "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);
            }
        }
        

        關於控制器

        處理「關於我們」頁面邏輯。

        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

        應用程式的入口點。

        /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)點

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

        缺點

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

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

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

        對於更複雜的項目,存在大量邏輯重用或多個入口點,前端控制器或完整MVC架構等模式可能更合適。

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

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

        熱AI工具

        Undress AI Tool

        Undress AI Tool

        免費脫衣圖片

        Undresser.AI Undress

        Undresser.AI Undress

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

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

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

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

        PHP中使用基本數(shù)學運算的方法如下:1.加法用 號,支持整數(shù)和浮點數(shù),也可用於變量,字符串數(shù)字會自動轉(zhuǎn)換但不推薦依賴;2.減法用-號,變量同理,類型轉(zhuǎn)換同樣適用;3.乘法用*號,適用於數(shù)字及類似字符串;4.除法用/號,需避免除以零,並註意結果可能是浮點數(shù);5.取模用%號,可用於判斷奇偶數(shù),處理負數(shù)時餘數(shù)符號與被除數(shù)一致。正確使用這些運算符的關鍵在於確保數(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í)行鍵值設置與獲取,推薦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

        如何設置PHP時區(qū)? 如何設置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