一步步编写PHP的Framework(一)
框架这个概念可能一开始就吓坏了很多PHP的Coder,其实你只要把一个Framework想明白了,其实也没啥,只是别人功能更复杂,细节考虑的更多,代码质量更高。
框架最重要的一个文件为入口文件,基本上相当于一个总控开关,所有的请求都需要经过这个文件:
1 |
2 | define('APP_PATH',dirname(__FILE__).'/..'); //应用路径 |
3 | include APP_PATH.'/Library/Toper/Core/FrontController.class.php';//导入前端控制器 |
4 | $frontController = Tp_FrontController::getInstance(); |
5 | $frontController->run(); |
这个文件可以只做一件事情,那就是将控制权交给框架。
大家都知道,一个MVC的应用,所有的请求都必须经过控制器,所以下面我们来编写一个比较简单的控制器:
1 |
2 | class Test_IndexController extends Tp_Controller { |
3 | public function indexAction() { |
4 | echo 'a'; |
5 | } |
6 | } |
这个类只需要继承自Tp_Controller即可,Tp_Controller是Toper的控制器的基类,只要你继承它,你就可以使用框架帮你写好的一系列方法,这样就可以大大减少你自己的工作量了,并且按照规范,函数名也是有意义的,如刚才写的这个函数代表的意义就是访问Test模块IndexContrller这个控制器下面的index这个Action。
那么如何使用模型呢?实际上非常简单。
1 |
2 | class Test_IndexModel extends Tp_Model { |
3 | public function test() { |
4 | return 'test'; |
5 | } |
6 | } |
原理和控制器一样,也是继承框架的类,然后自定义方法,这个地方的方法名没有限制,可以随意。
那么编写模型之后怎么在控制器中调用呢,其实方法就是在控制器中实例化这个模型类,然后调用相应的方法即可,所以修改刚才这个控制器的类:
1 |
2 | class Test_IndexController extends Tp_Controller { |
3 | public function indexAction() { |
4 | $model = new Test_IndexModel(); |
5 | echo $model->test(); |
6 | } |
7 | } |
为什么没有include语句?
那是因为框架帮你做完了这一切,当然,框架不是万能的,当你比较熟悉之后,也可以选择不使用自动导入。
刚才我们所做的一切都没有View,那么怎么编写View呢,实际上就是一个HTML文件,框架在View这一层一般是实现标签库,标签库帮你完成一些原来需要用PHP代码才能完成的事情,比如循环遍历,没有标签库可能你就只能在这个View的文件中使用,这样对于代码分离很不好。
如果你对标签库没有概念,那么你直接看下面的代码吧!
1 |
2 |
3 |
|
4 |
5 |
6 |
|
7 |
8 |
这是一段没有什么难度的代码,唯一的难度可能就是这个print标签,这个标签是框架定义的,它的功能很简单,就是打印一个字符串,你可能觉得这样没什么意义,因为我使用PHP代码也可以很轻易的完成这个功能,但是你想像一下一个做前端的为什么还需要懂PHP呢,如果使用标签库,那么前端开发人员就可以像使用HTML标签一样操作数据了。
我们知道所有的请求都必须要通过控制器,所以外部是无法直接访问这个视图文件的,所以又需要修改控制器的代码了!!!
1 |
2 | class Test_IndexController extends Tp_Controller { |
3 | public function indexAction() { |
4 | $model = new Test_IndexModel(); |
5 | echo $model->test(); |
6 | $this->_display('Test.test'); |
7 | } |
8 | } |
如果你学过smarty,你可能对display方法很熟悉,实际上这个函数的功能就是显示一个模板文件!!!
好了,一个基本的MVC应用就搭起来了,不是很难吧!!!
如何查看效果呢,有两种方式:
1.CGI:
打开浏览器,浏览器访问时,如果您的域名是:localhost/testframework,那么您可以使用localhost/testframework/Public/index.php/Test/Index/index来访问,如果您设置了虚拟主机,如www.a.com,那么只需要使用www.a.com/Test/Index/index就可以访问了;
2.CLI:
通过命令行直接执行PHP脚本时,在Public目录下面使用php index.php m:test c:Index a:index。
刚才是使用框架实现了一个MVC的应用,那么怎么不使用框架来构建一个MVC应用呢?
下次再讲,请继续关注!!!!

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

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

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

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

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

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.
