Home Backend Development PHP Tutorial PHP简单的MVC框架实现方法_php实例

PHP简单的MVC框架实现方法_php实例

Jun 07, 2016 pm 05:10 PM

在PHP中使用MVC越来越流行了,特别是在一些开源的框架当中。

1.概述

  MVC全名是Model View Controller,是模型(model)-视图(view)-控制器(controller)的缩写,一种软件设计典范,用一种业务逻辑、数据、界面显示分离的方法组织代码,将业务逻辑聚集到一个部件里面,在改进和个性化定制界面及用户交互的同时,不需要重新编写业务逻辑。MVC被独特的发展起来用于映射传统的输入、处理和输出功能在一个逻辑的图形化用户界面的结构中。

2.代码结构

3.代码实现

<&#63;php
    //function.php 
  //控制器调用函数
  function C($name, $method){
    require_once('libs/Controller/'.$name.'Controller.class.php');
    //$testController = new testController();
    //$testController->show();
    eval('$obj = new '.$name.'Controller(); $obj->'.$method.'();');
  }
  //模型调用函数
  function M($name){
    require_once('libs/Model/'.$name.'Model.class.php');
    eval('$obj = new '.$name.'Model();');
    return $obj;
  }
  //视图调用函数
  function V($name){
    require_once('libs/View/'.$name.'View.class.php');
    eval('$obj = new '.$name.'View();');
    return $obj;
  }
  //过滤非法值
  function daddslashes($str){
    return (!get_magic_quotes_gpc())&#63;addslashes($str):$str;
  }
&#63;>
<&#63;php
//test.php
/*
第一步 浏览者 -> 调用控制器,对它发出指令
第二步 控制器 -> 按指令选取一个合适的模型
第三步 模型 -> 按控制器指令取相应数据
第四步 控制器 -> 按指令选取相应视图
第五步 视图 -> 把第三步取到的数据按用户想要的样子显示出来
*/
require_once('View/testView.class.php');
require_once('Model/testModel.class.php');
require_once('Controller/testController.class.php');
$testController = new testController();
$testController->show();
&#63;>
<&#63;php
//testController.class.php
/*
控制器的作用是调用模型,并调用视图,将模型产生的数据传递给视图,并让相关视图去显示
*/
  class testController{
    function show(){
      /*$testModel = new testModel();
      $data = $testModel->get();
      $testView = new testView();
      $testView->display($data);*/
      $testModel = M('test');
      $data = $testModel->get();
      $testView = V('test');
      $testView->display($data);
    }
  }
&#63;>
<&#63;php
//testModel.class.php
/*
模型的作用是获取数据并处理,返回数据
*/
  class testModel{
    function get(){
      return "hello world";
    }
  }
&#63;>
<&#63;php
//testView.class.php
/*
视图的作用是将获得的数据进行组织,美化等,并最终向用户终端输出
*/
  class testView{
    function display($data){
      echo $data;
    }
  }
&#63;>
Copy after login

 运行结果:


PHP中的MVC

MVC[1]在软件工程中是一种软件的架构。从php的角度来讲MVC有一些不同。

Model(模型),程序应用功能的实现,程序的逻辑的实现。在PHP中负责数据管理,数据生成。

View(视图),图形界面逻辑。在PHP中负责输出,处理如何调用模板、需要的资源文件。

Controller(控制器),负责转发请求,对请求处理。在PHP中根据请求决定调用的视图及使用的数据。

为什么使用MVC

MVC的主要作用是为了将代码分层、分类。

MVC的主要目的是为了解决Web开发中分离开发与设计工作,使其工作相对独立。

在这样的过程中还发现了其他的一些优点,网站的目录结构更加清晰,网站更易维护与扩展,可以实现模块的复用。

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)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Two Point Museum: All Exhibits And Where To Find Them
1 months 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)

Working with Flash Session Data in Laravel Working with Flash Session Data in Laravel Mar 12, 2025 pm 05:08 PM

Laravel simplifies handling temporary session data using its intuitive flash methods. This is perfect for displaying brief messages, alerts, or notifications within your application. Data persists only for the subsequent request by default: $request-

cURL in PHP: How to Use the PHP cURL Extension in REST APIs cURL in PHP: How to Use the PHP cURL Extension in REST APIs Mar 14, 2025 am 11:42 AM

The PHP Client URL (cURL) extension is a powerful tool for developers, enabling seamless interaction with remote servers and REST APIs. By leveraging libcurl, a well-respected multi-protocol file transfer library, PHP cURL facilitates efficient execution of various network protocols, including HTTP, HTTPS, and FTP. This extension offers granular control over HTTP requests, supports multiple concurrent operations, and provides built-in security features.

Simplified HTTP Response Mocking in Laravel Tests Simplified HTTP Response Mocking in Laravel Tests Mar 12, 2025 pm 05:09 PM

Laravel provides concise HTTP response simulation syntax, simplifying HTTP interaction testing. This approach significantly reduces code redundancy while making your test simulation more intuitive. The basic implementation provides a variety of response type shortcuts: use Illuminate\Support\Facades\Http; Http::fake([ 'google.com' => 'Hello World', 'github.com' => ['foo' => 'bar'], 'forge.laravel.com' =>

12 Best PHP Chat Scripts on CodeCanyon 12 Best PHP Chat Scripts on CodeCanyon Mar 13, 2025 pm 12:08 PM

Do you want to provide real-time, instant solutions to your customers' most pressing problems? Live chat lets you have real-time conversations with customers and resolve their problems instantly. It allows you to provide faster service to your custom

PHP Logging: Best Practices for PHP Log Analysis PHP Logging: Best Practices for PHP Log Analysis Mar 10, 2025 pm 02:32 PM

PHP logging is essential for monitoring and debugging web applications, as well as capturing critical events, errors, and runtime behavior. It provides valuable insights into system performance, helps identify issues, and supports faster troubleshoot

Explain the concept of late static binding in PHP. Explain the concept of late static binding in PHP. Mar 21, 2025 pm 01:33 PM

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

HTTP Method Verification in Laravel HTTP Method Verification in Laravel Mar 05, 2025 pm 04:14 PM

Laravel simplifies HTTP verb handling in incoming requests, streamlining diverse operation management within your applications. The method() and isMethod() methods efficiently identify and validate request types. This feature is crucial for building

Discover File Downloads in Laravel with Storage::download Discover File Downloads in Laravel with Storage::download Mar 06, 2025 am 02:22 AM

The Storage::download method of the Laravel framework provides a concise API for safely handling file downloads while managing abstractions of file storage. Here is an example of using Storage::download() in the example controller:

See all articles