一個簡易的MVC模型

來源:互聯網
上載者:User
MVC 是一種用於將應用程式的邏輯層和表現層分離出來的軟體方法。在實踐中,由於這種分離,所以你的頁面中只包含很少的 PHP 指令碼。
模型 代表你的資料結構。通常來說,模型類將包含協助你對資料庫進行增刪改查的方法。
視圖 是要展現給使用者的資訊。一個視圖通常就是一個網頁。
控制器 是模型、視圖以及其他任何處理 HTTP 要求所必須的資源之間的中介,並產生網頁。
自製一個簡易的MVC模型。
1.入口檔案index.php
唯一一個讓瀏覽器直接請求的指令檔
2.控制器controller
協調模型和視圖
3.模型model
提供資料,儲存資料
4.視圖view
負責顯示網頁
5.動作action
控制器中的方法,用於被瀏覽器請求

--------------------檔案夾結構-------------------------

主資料夾mvc_demo

--controllers

--ArticleController.php

--UserController.php

--models

--UserModel.php

--views

--User/index.php

index.php //入口檔案

-----------------------------------------------------------

index.php

$a();

controllers/UserController.php
UserController{public function index(){//echo "這是User控制器的index方法";//包含檔案並執行個體化一個模型include './models/UserModel.php';//通過模型擷取資料$model = new UserModel();$str=$model->getUser();include './views/User/index.php';}}

models/UserModel.php

views/User/index.php

在瀏覽器中輸入http://localhost:8080/mvc_demo/index.php?c=User&a=index,顯示效果使用者helen。

以上就介紹了一個簡易的MVC模型,包括了UserControl方面的內容,希望對PHP教程有興趣的朋友有所協助。

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.