PHP中MVC体系结构的基础知识

来源:互联网 发布:大淘客cms有什么用 编辑:程序博客网 时间:2024/05/18 05:02
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 functionoutput()
{
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 running first_controller.php is:

<?php
class Controller
{
private $model;
public function__construct($model)
{
$this->model = $model;
}
}
?>

A simple way to understand how MVC works is given below.
1) A user interacts with View.
2) The Controller handles the user input, and sends the information to the model.
3) Then the Model receives the information and manipulates it (either saving it or updating it by communicating with the database).
4) The View checks the state of the Model and responds accordingly (lists updated information).
In the following code snippet, by running first_example.php we can see our MVC architecture work with PHP.
To run first_example.php, you must type:

<?php
$model = new Model();
$controller = new Controller($model);
$view = new View($controller, $model);
echo$view->output();
?>
原创粉丝点击