#Starting point: I personally understand that developing your own framework is also a way to exercise nativeness. After writing it, it will be much easier to look at other frameworks. If you encounter a framework that has no documentation or very little documentation, you can follow this idea and add it. Or just bring the native one and do it. This was the case when I first came into contact with yii2. I started working without even having time to read the documentation. It was basically purely native and I used the csrf and dual languages of yii2....
How to receive and print parameters. For example, the method of obtaining the configuration file is based on the passed parameters. Idea
1. Generally, it is first determined whether the file exists. If it exists, it should be included, and if it should be new, it should be new. Then save it into attributes (caching)
2. Mainly the calling process: everything that needs to be put into the run method
1. Write the entry file (steps 2-5 are all written in the entry file)
2. Define constants (which are directories at each level of the framework, so you don’t have to write a long list each time)
//定义框架根目录 //定义核心文件目录路径 //应用目录(包含控制器、模型等) //是否开启调试 //如果DEBUG为true,将错误显示打开。否则不显示
3. Introduction of function library
##
//加载公共方法 //加载核心文件
4. Automatic loading
Determine whether the class to be loaded exists.Tip: It has been saved in the attribute. In fact, it is judged by this attribute.
Returns true if it exists; Load if it does not exist.Tips: Backslash replacement
If you want to load a file, just include.Note: This file is found from the root directory
If not, it will return falseNote: After writing, create the lib directory and introduce the routing class. Test
5, Startup Framework
//调用核心文件中的run方法(我的核心文件是core/init.php)
6, Route Analysis Trilogy
6.1, Hidden Entrance File6.2. Get the parameters of url If it exists, parse it Normal situation, first convert it to the array (for convenience of processing), and verify the abnormal situation (such as: only in url The controller has been entered, but there is no input method. Give the method a default value) Extra parameters on the url Normal situation Abnormal situation, he entered It is an odd number It does not exist and is given to the default value6.3. Return the corresponding controller and method (in fact, it is saved in the attribute in 6.2, which can be regarded as returned)7. Load the controller
The controller must be loaded during run()Put the controller name parsed out of the route into the path of the controller file. Determine whether the controller file exists. If it exists, use new. If it does not exist, an error will be thrownNote: The concept of module is introduced here\app is a module, not an application, one layer less than a mature framework8. Return results
Run it and give it a try. In fact, the archiving framework has been completed at this point. The next step is to extend the framework9. Connect the model class in the controller
Create a model class in lib for connecting to the databaseCreate a new model class in the controller and write sql to operate the database10. Write the view class
The assign and display methods are written in the initialization (core/init.php) class.Note: Assign the value first, then include the template file
Then the controller inherits this class and calls it.11. Write configuration class## Create a new configuration file class
Method to write a single configuration file (
Tip: Since they obtain it, passing parameters is indispensable. 2 parameters, configuration items, configuration file name. Print the parameters, and then conceive)
# Write how to get all configuration files
reminder: new configuration file directory is created to put various configuration files (such as databases, routes, routes, routing Yes, log...)Test: The configuration file is working properly12. Write log class
//1、判断配置文件是否存在,存在就include //2、判断配置项是否存在,存在直接给缓存到属性中 //3、判断属性中有无缓存,有就直接返回属性中存的
13、使用composer:编写json文件,自己的框架上到github上供人家使用
第1个类库报错的地址:https://github.com/filp/whoops
第2个类库酷炫的打印变量:"s
"symfony/var-dumper":"*"//更帅气变量输出的效果 ymfony/var-umper":"*"//更帅气变量输出的效果
第3个类库数据库的地址:http://medoo.lvtao.net/1.2/doc.php
第4个类库模板引擎的地址:https://twig.symfony.com/doc/2.x/intro.html#installation
镜像网址:https://pkg.phpcomposer.com/
注意:下图很重要
14、使用medoo数据库类
如果自己封装pdo可能会有什么想象不到的漏洞,这样我们就不如直接用现成的类库加载
http://medoo.lvtao.net/
思路:先将原来继承的pdo换成该类库,在封装模型控制器调用
15、模板引擎类库
思路:修改display方法,改成类库的,还有模板布局
剩下其余的扩展就因人而异了,想不到的话,可能去参考成熟的框架
这里写2个封装好的方法
封装post接收方法(给大家做参考思路)
封装跳转方法
更多教程:《php教程》
The above is the detailed content of It's super simple to develop your own PHP framework. It's not difficult at all!. For more information, please follow other related articles on the PHP Chinese website!