php实现简单的MVC框架实例
这篇文章主要介绍了php实现简单的MVC框架,较为详细的分析了php实现MVC框架的相关实现技巧与注意事项,具有一定参考借鉴价值,需要的朋友可以参考下
本文实例讲述了php实现简单的MVC框架。分享给大家供大家参考。具体如下:
在开始之前需要知道的知识
1.php基础知识
2.单一入口, 不知道的可以看看这里 ()
具备以上两点, 那我们就可以开始啦. 哈哈!
先来说一下程序的执行流程
首先有个入口文件, 然后初始化一些程序, 之后根据请求调用不同的类和方法
首先我们弄一个入口文件 Index.php 来看看代码
Run(); ?>
代码没什么特别的, 首先先引入Init.php文件 然后实例化一个类
然后调用该类的Run()方法 这里我们把这个类叫做控制器
既然引入了Init.php文件, 那么我们继续看看Init.php文件的源码
分析一下代码, 如果你懂了, 可以略过这一步, 继续往下看哦.
首先 设置字符集, 然后判断如果没有定义常量"ROOT_PATH"那么就定义它
然后就是引入一些文件, 首先是配置文件, 控制器类文件, 视图类文件, 模型类文件
同理既然引入了文件,那么我们就打开文件看看代码, 先来看Config.php文件
1, //URL模式, 1普通模式, 2 PATH_INFO模式 'DEFAULT_CONTROL' => 'welcome', //默认调用的控制器 'DEFAULT_ACTION' => 'index', //默认执行的方法 ); ?>
没什么特别的就是一个数组, 有三个值, 暂时先这样, 以后有需要在来增加
那么好, 我们继续看Controll.class.php
Analysis(); //开始解析URL获得请求的控制器和方法 $control = $_GET['c']; $action = $_GET['a']; //这里构造出控制器文件的路径 $controlFile = ROOT_PATH . '/Controllers/' . $control . '.class.php'; if(!file_exists($controlFile)) //如果文件不存在提示错误, 否则引入 { exit('控制器不存在' . $controlFile); } include($controlFile); $class = ucwords($control); //将控制器名称中的每个单词首字母大写,来当作控制器的类名 if(!class_exists($class)) //判断类是否存在, 如果不存在提示错误 { exit('为定义的控制器类' . $class); } $instance = new $class(); //否则创建实例 if(!method_exists($instance, $action)) //判断实例$instance中是否存在$action方法, 不存在则提示错误 { exit('不存在的方法' . $action); } $instance->$action(); } protected function Analysis() { global $C; //包含全局配置数组, 这个数组是在Config.ph文件中定义的 if($C['URL_MODE'] == 1) //如果URL模式为1 那么就在GET中获取控制器, 也就是说url地址是这种的?c=控制器&a=方法 { $control = !empty($_GET['c']) ? trim($_GET['c']) : ''; $action = !empty($_GET['a']) ? trim($_GET['a']) : ''; } else if($C['URL_MODE'] == 2) //如果为2 那么就是使用PATH_INFO模式, 也就是url地址是这样的 控制器/方法/其他参数 { if(isset($_SERVER['PATH_INFO'])) { //$_SERVER['PATH_INFO']URL地址中文件名后的路径信息, 不好理解, 来看看例子 //比如你现在的URL是 那么你的$_SERVER['PATH_INFO']就是空的 //但是如果URL是 //现在的$_SERVER['PATH_INFO']的值将会是 index.php文件名称后的内容 /abc/123/ $path = trim($_SERVER['PATH_INFO'], 'http://www.jb51.net/'); $paths = explode('http://www.jb51.net/', $path); $control = array_shift($paths); $action = array_shift($paths); } } //这里判断控制器的值是否为空, 如果是空的使用默认的 $_GET['c'] = $control = !empty($control) ? $control : $C['DEFAULT_CONTROL']; //和上面一样 $_GET['a'] = $action = !empty($action) ? $action : $C['DEFAULT_ACTION']; } } ?>
注释写的很清楚, 这里我就不多说了, 做到这, 你就可以建立一个Controller目录, 然后在目录中建立welcome.class.php文件
写入如下内容
允许程序你将会看到Hello
然后在写一个方法
希望本文所述对大家的php程序设计有所帮助。
,
Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

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

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.
