PHP Framework Principles_PHP Tutorial

WBOY
Release: 2016-07-13 10:29:00
Original
765 people have browsed it

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>?> 
Copy after login

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>
?> 
Copy after login
一个面向过程的单点入口框架就完成了, 是不是有觉得每次都在地址栏带一个 get 不方便? 那我们可以换一个写法,例如 tp 框架最喜欢用的 http://localhost/index.php/news/list后面的 /news/list 由程序转成 php 路径包含进来就可以了。在 Apache 环境中,这个 /news/list 可以由服务器变量的 PATH_INFO 取得,如果没有的话。也可以用 REQUEST_URI 取得接近的 ,IIS 下面,有 HTTP_X_REWRITE_URL 可以取得这个值 ,自从单点入口模式出现之后,而且oop开发模式从php5开始大行其道,各种oop设计的框架让我们眼花缭乱,但是万变不离其宗,依然是什么入口方式,路径结构是什么样的,文件名的命名规则,用什么样的访问方式,可以运行哪个程序。用oop开发的框架,不外乎就是把主程序改写成为了一个类,

例如:

//包含共用文件,实例化各个类啥的
页面->初始化();

//把用户发来的网址转成要包含的路径
页面->处理网扯();

//在这里包含程序运行
页面->运行()

//输出模板
页面->输出()

各种各样的框架只是为我们准备了一个规矩罢了。。在我们的开发累计的过程中,我们常常会把一些常用的类封装成类,例如,数据库类,文件上传类,图片处理类,邮件收发类,远程访问类,各种接口类……这个时候,我们就会希望框架能给我们提供一个好一点调用类的方法, 也就是所谓的“扩展性” ,比如 TP 框架的 Db 类 。如果不用其自带的类库只用它们的核心框架,其实几个文件就够了。 TP 框架支持三种访问格式。
/news/list
/index.php/news/list
/index.php?m=news&a=list 第一种需要服务器的 urlrewrite 支持,后面两种可以直接用, 事实上,Zend 框架也差不多 文件的包含方式是。以类的形式包含,执行的其实是:/文件夹/对象/方法,这种做法有优势。因为在同一个功能中,相似的代码很多,封装到同一个类里面,可以更高效的重复使用代码 ,
<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>
Copy after login

还可以利用构造函数等,使每一个功能,在刚进来的时候就都做了同一件事情。以上就是简单框架的理论。

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/780017.htmlTechArticle本文主要来聊聊框架理论,但不针对任何一款框架,不过任何一款框架都离不开这个理论,首先我们了解下框架的来龙去脉,任何技术的出...
Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!