This article mainly talks about framework theory, but it does not target any framework. However, any framework is inseparable from this theory. First, we understand the ins and outs of the framework. The emergence of any technology is to solve Regarding a certain issue, a previous blog has talked about smarty, which exists to better separate HTML and PHP. The so-called "framework" is a self-restraint behavior in order to unify writing formats and access methods. In fact, according to this statement, each of us has basically used a self-defined framework to a greater or lesser extent. For example, when we have not used Before Framework developed a project by itself, sometimes it couldn't be completed in one day. In order to prevent confusion and to better remember, we would plan directories and programs, subconsciously classify the programs and put them in different folders, so Framework It came into being. For example, we once made a CMS system. If we took over a similar project again, would you rewrite the code again? The answer is definitely not, but if it is someone else's project and you use it to modify it, it will actually be the same. This is a very painful thing, because you don’t know what the rules of his CMS are. Even if your project lasts for a long time, it is easy to forget if you don’t have fixed specifications. So how can you ensure that you The code you write each time can follow certain standards. Pick out the things that each project will use, and pick out the directory structure. No matter what projects you write in the future, you can write on this basis, and then your own framework will come out.
But the framework alone is not perfect, so what capabilities should the framework have? Instead of saying this, we might as well think about what the code we usually write will definitely do, and what should be solved first? In order to reduce path problems, the arrangement of the directory structure is actually very important. Sometimes it is a pain to move files after including them. What is the best way? Absolute path, but at the same time there is a file similar to E :www Such a question, but we can get this value through the predefined variable $_SERVER["DOCUMENT_ROOT"], we can define it as a constant, define("ROOT_PATH", $_SERVER["DOCUMENT_ROOT"]);include ROOT_PATH. "/lib/mysql.php"; No matter how you move a folder like this, there will be no problem. Then there is a fixed writing method. This writing method exists to solve the path problem and is used on almost every page. There are also Template output and database connection, then we can encapsulate or separate these codes and include them on each page. These separated parts are actually a small framework. Why do we say this? If we include them, For example, the smarty template must be an instantiated object. The quantity name has been fixed, maybe $smarty. If we include such a file, this quantity cannot be assigned and used again, so the following part of the code cannot be used. , since the included classes, such as data class, upload class, image class, paging class location, have been hard-coded in this public file, this directory must exist in order not to change the code. The directory format is fixed and the code writing method is constrained. A framework is formed.
Over the years, programmers have summarized exchanges and development experience. We have summarized some excellent writing methods. The most classic one is single point entry. What is single point entry? Previously we summarized some requirements for almost every program. There are still some problems here for the functions used. For example, we still don’t know what the root directory is before the public file is included. If we put a public file under each folder, there will be code duplication problems. If we modify it one day, we will need to all To modify, you need to find out how many such files are needed. Now multiple programs contain one program, and then the user accesses n programs to complete each function, so the programmer wonders if I can use one program to contain these different functions. , the user only needs to access this program, so the single-point entry mode appeared. Write the parts that each program uses in the index.php on the homepage of the website, and then judge it based on a certain quantity, such as a get quantity. Which program is actually being executed currently is included and run by index.php. This is a program. The way to complete all functions is called a single point of entry, so this entry program and its corresponding directory structures become a framework.
For security reasons, directories are often fixed when including files, otherwise vulnerabilities will easily occur. Therefore, restrictions are often added to the beginning and end of the path. For example
<?<span>PHP </span><span>include</span> "./app/" . <span>$_GET</span>['url'] . ".php"<span>; </span>?>
Then the path can only be written like index.php?url=news/list, which actually contains /app/news/list.php. Of course, in the actual situation, you must also check whether the program file exists. class.
完整一点的话。我们可以这样写这个入口文件。<?<span>php </span><span>//</span><span>这里写绝对路径 //这里写数据库连接 //这里写模板初始化,配置 //这里判断连接变量 //这里包含文件进来运行 //这里输出模板 //这里关闭数据库</span> ?>
例如:
//包含共用文件,实例化各个类啥的
页面->初始化();
//把用户发来的网址转成要包含的路径
页面->处理网扯();
//在这里包含程序运行
页面->运行()
//输出模板
页面->输出()
<span>比如这样 </span><span>class</span><span> NewsAction { </span><span>public</span> <span>function</span><span> head() { 在这里处理每一页头部 } </span><span>public</span> <span>function</span><span> index() { </span><span>$this</span>-><span>head(); 在这里处理这一页 } </span><span>public</span> <span>function</span><span> show() { </span><span>$this</span>-><span>head(); 在这里处理这一页 }</span>
还可以利用构造函数等,使每一个功能,在刚进来的时候就都做了同一件事情。以上就是简单框架的理论。