PHP中MVC體繫結構的基礎知識____PHP

來源:互聯網
上載者:User
MVC, which stands for Model-View-Controller, is a really good way to develop clean, scalable, powerful and fast applications in the least amount of time and with the least effort. Before exploring MVC, this article begins with a brief introduction to PHP.

PHP is an open source programming language that was developed in 1994 by Rasmus Lerdorf. PHP originally stood for Personal Home Pages but now stands for Hypertext Preprocessor, which is a recursive acronym. It is a loosely typed server-side scripting language used to build Web applications. A scripting language is a language that does not need to be compiled before use. And ‘loosely typed’ languages are those in which there is no need to define the type of a variable.
Today, more than 20 million domains use PHP, including Facebook and Yahoo.
PHP is easily embedded with HTML, and is used to manage dynamic content and the databases of websites or, we can say, Web applications. We can use PHP with many popular databases like MySQL, PostgreSQL, Oracle, Sybase, Informix and Microsoft SQL Server.

How PHP works on servers
When a client (browser) sends a request for a PHP page to the server, the server reads the requested file from the disk (storage) and sends this file to the interpreter. The interpreter then runs the PHP code, fetches the DB data (if required) and puts it into HTML tags. Once the interpreter completes its tasks, it sends the result back to the server, which sends this data to the client (browser) that made a request for this page.

MVC architecture with PHP
The Model-View-Controller concept involved in software development evolved in the late 1980s. It’s a software architecture built on the idea that the logic of an application should be separated from its presentation. A system developed on the MVC architecture should allow a front-end developer and a back-end developer to work on the same system without interfering with each other.

涉及軟體開發的模型 - 視圖 - 控制器概念在20世紀80年代後期發展。它是建立在應用程式的邏輯應該與其呈現分離的思想基礎上的軟體體繫結構。在MVC架構上開發的系統應該允許前端開發人員和後端開發人員在相同的系統上工作而不會相互幹擾。

Model
Model is the name given to the component that will communicate with the database to manipulate the data. It acts as a bridge between the View component and the Controller component in the overall architecture. It doesn’t matter to the Model component what happens to the data when it is passed to the View or Controller components.

Model是與組件通訊的名稱,用於處理資料。它充當整個架構中View組件和Controller組件之間的橋樑。它與Model組件無關,當它傳遞給View或Controller組件時,資料會發生什麼變化。
The code snippet for running first_model.php is:

<?php class Model { public $string; public  function __construct() { $this->string = “Let’s start php with MVC”; } } ?>

View
The View requests for data from the Model component and then its final output is determined. View interacts with the user, and then transfers the user’s reaction to the Controller component to respond accordingly. An example of this is a link generated by the View component, when a user clicks and an action gets triggered in the Controller.

視圖請求來自模型組件的資料,然後確定其最終輸出。視圖與使用者互動,然後將使用者的反應傳遞給控制器組件以作出相應的響應。其中的一個例子就是View組件產生的連結,當使用者單擊並在Controller中觸發某個操作時。
To run first_view.php, type:

<?php class View { private $model; private $controller; public  function __construct($controller,$model) { $this->controller = $controller; $this->model = $model; } public  function output() { return “<p>” . $this->model->string . “< /p >”; } } ?>

Controller
The Controller’s job is to handle data that the user inputs or submits through the forms, and then Model updates this accordingly in the database. The Controller is nothing without the user’s interactions, which happen through the View component.

控制器的工作是處理使用者通過表單輸入或提交的資料,然後Model在資料庫中相應地進行更新。控制器沒有使用者的互動,通過View組件發生。
The code snippet for runnin

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.