Home Backend Development PHP Tutorial php实现最简单的MVC框架实例教程_php技巧

php实现最简单的MVC框架实例教程_php技巧

May 16, 2016 pm 08:36 PM
mvc php accomplish frame

本文以一个实例的形式讲述了PHP实现MVC框架的过程,比较浅显易懂。现分享给大家供大家参考之用。具体分析如下:

首先,在学习一个框架之前,基本上我们都需要知道什么是mvc,即model-view-control,说白了就是数据控制以及页面的分离实现,mvc就是这样应运而生的,mvc分为了三个层次,而且三个层次各司其职,互不干扰,首先简单介绍下,各个层次:view即是视图,也就是web页面,control即是控制器 向系统发出指令的工具,model 简单说是从数据库中取出数据进行处理。

MVC的工作流程如下:

1. 浏览者->调用控制器,对此发出指令

2. 控制器->按指令选取一个合适的模型

3. 模型->按照控制器指令选取相应的数据

4. 控制器->按指令选取相应的视图

5. 视图->把第三步取到的数据按用户想要的样子显示出来

简单地实例开发如下,首先进行第一个控制器的开发 我们在此命名规范如下testController.class.php

<&#63;php
class testController{
function show(){
 
}
}
&#63;>
Copy after login

其次书写一个简单地模型如下testModel.class.php

<&#63;php
 
class testModel{
function get(){
return "hello world";
 
}
}
&#63;>

Copy after login

第一个视图文件的创建testView.class.php 是为了呈现数据所存在的

<&#63;php
class testVies{
  function display($data){
     echo $data;
 
  }
 }
&#63;>
Copy after login

下面我们要做的就是按照之前所说的五步进行程序的测试:代码如下 测试文件的建立test.php

<&#63;php
require_once('testController.class.php');
require_once('testModel.class.php');
require_once('testView.class.php');
$testController = new testController();//调用控制器
$testController->show();
&#63;>
Copy after login

<&#63;php
class testController{
  function show(){
      $testModel = new testModel();//选取合适的模型
      $data = $testModel->get();//获取相应的数据
      $testView = new testView();//选择相应的视图
      $testView->display($data);//展示给用户
  }
}
&#63;>
Copy after login

而后我们浏览器打开test.php 会显示为hello world,说明我们已经成功了。

注:本文实例仅为框架结构,具体的功能读者可以自行添加。希望本文所述实例对大家PHP程序设计框架的学习有所帮助。

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP Working with Database CakePHP Working with Database Sep 10, 2024 pm 05:25 PM

Working with database in CakePHP is very easy. We will understand the CRUD (Create, Read, Update, Delete) operations in this chapter.

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

In this chapter, we are going to learn the following topics related to routing ?

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

See all articles