I have been learning PHP by myself for more than half a year, intermittently, but in the end I firmed up my mind and continued PHP, so I wrote this PHP blog to find a stable PHP job without asking for a high salary. , but looking for a place to take shelter. I can understand most English documents. I am not stupid and I love learning. If you are interested, please contact me! Come if you are sincere! qq:240382473
I will release all key codes and documentation instructions in 3-5 times, and all styles in the blog backend will be applied to the blog garden!
Description:
1. It does not completely adopt the MVC architecture, but the concept is like this. Because it is not possible to write a very stable MVC architecture.
2. JQUERY AJAX is almost never used because I am not very familiar with it and can’t use it freely. You can use AJAX for guestbooks, no problem.
3. There are several public classes, and other codes are all handwritten. Please point out any shortcomings. Thank you very much.
4. Criticism and guidance are welcome, but please give your reasons.
Closer to home: Let’s look at the database architecture first
The engines of these tables are all MYISAM, which is convenient for access. (The yellow key represents the primary key; the blue diamond represents a non-empty field; the white diamond represents a null field) The links in the picture only represent a potential relationship between them and cannot be associated during operation. Because the search engine is MyISAM . Therefore, joint queries and multi-table operations are required.
I will select the most important special fields in the post and category tables to explain in detail, and the others will be mentioned as important.
post:
post_id
category_id varchar(10) This is the category used to index blog posts. The category_id here is also a string type, so multiple categories can be set for each blog post.
type varchar(20) This field is used to distinguish between post, article, and diary; it can also be set to postDraft, articleDraft;
visiable Whether the blog post is visible
Other commonly used fields include title, content, creation time, last modification time, number of views, number of comments, tags, allowed comments, and some reserved fields.
category:
parent, count_child_number, count_parent_number for future expansion
type can set the categories of photo albums, blog posts, and diaries respectively
Other common fields such as name, description, creation time, visibility
comment:
address user IP
user_agent User browser type
Other fields are omitted...
Server Architecture
PHP5.4.2 + MYSQL 5.523 + APACHE 2.2.22 + Windows NT ARIST-PC 6.1 build 7600 (Windows 7 Home Basic Edition) i586 (local)
Blog Architecture
Backend directory:
Backend directory description:
assert stores various resources js, css, image
class stores our classes, commonly used classes such as database operation classes, paging classes, and most of our models. . .
extention stores extensions such as mce's rich editor
config stores our configuration information
templates stores all templates (smarty is not used)
upload stores photos and other files
There will be some similar controllers in the admin root directory, such as index.php, post.php, article.php, photo.php
Let’s take a look at admin/config/config.php
这里我们定义了一些基本常量,和几个函数。
__autoload() 函数加载 admin/class/ 中的所有类
用 hasher() 函数加密了一个 88位的 不可逆密码, 登录过程就是用config.php 中的常量和 hasher( ) 函数来进行验证。
来看我们的 admin/index.php 后台控制器 这个控制器主页 显示一些博客的相关数据
这个设计模式是从一个老外那里学的!
原理就是:
首先我们加载我们的config.php, 初始化session变量,获得 $action 这个重要变量的值;
然后我们判断 $action 和 $username 的值, 如果用户没有登录以及用户名为空,返回登录页面;
如 果用户正确输入了用户名和密码,则注册一个session 变量 $username,然后跳转到主页面 index.php, 这时我们会调用默认的 $action admin( ), 这个函数会加载一个模版admin.php;里面有个数组变量 $results['pageTitle'],以及我们的后台博客样式框架。
如果用户输入错了,则给出提示信息。
这个设计理念的核心就是, give {action} then {do something}
我们会在后面的代码中反复看到。
这个就是博客后台的框架样式,从博客园copy 来的,采用表格布局的,兼容的,可自定义其他样式的,简单的,实用的,可扩展的,完美后台框架。
这个样式在其他的浏览器中表现同样兼容,写这篇博文的时候,我已完成了部分功能。 下一篇:实现随笔,文章,日记 以及他们分类的CRUD。废话不多说了,上一篇有个核心概念就是 give action do something !
这篇我就用代码来解释这个概念是啥意思,先看我的 post.class.php . 这个文件是我们的数据层处理类。
简单介绍一下这个model 类,它继承了一个数据库基类来做crud 等常用操作, 每次初始化时就会初始化一个数据库对象 $db. 我们就用这个对象来操作我们的数据。
对于数据操作有2个重要方法 storePostFormValues( ) , storeDiaryFormValues( ),它们2个方法是数据流的开始。
还 有2个方法很有意思,addChildNumber( ), reduceChildNumber( ), 它们负责在插入或删除文档时的 一个暗箱操作。因为我的文档可以用多个分类,所以在操作文档的时候,要考虑到一个问题,就是 category 表中有个字段 记录了该分类下的 文档数量。所以要动态地改变这些数目的值。
下面配合 post.php 控制器,我们就可以开始我们数据的流程了(我的控制器还不是一个类,所以无法生成API文档。因为这还不是真正地MVC架构。)所以在MVC之前,这个也能更利于的理解MVC到底是神马东东,以及你自己如何去应用,写出自己的MVC。
以下的情形都是假设:
$action = "Give me a girlfriend from the sky!"; Let's pass in this controller and see what happens.
Copy code The code is as follows: if( !$username )
{
header("Location: index.php?action=login");
exit;
}
Here we have an important flow control statement switch, which means switch; so when the above $action = "Give me a girlfriend from the sky!"; when switch is passed in, there are only two possibilities, One is on and one is off. There is a bit of a pun here, and some students may see it. hey-hey!
Getting back to the topic: Take a look at how our switch switches these $actions on and off. It’s obvious that I won’t get a girlfriend from the sky, because there is no such switch in the controller, so I can only talk about the code.
Copy code The code is as follows:Each switch should define a default switch, so that when we don’t have a girlfriend, we can ensure that we still have gay friends.
How to pass in action?
Let’s look at such a URL, which is the navigation of our background framework. Post.php?action=isPost This is a standard action. Each of our URLs is actually composed of these actions. You can also add other parameters to it. in our url, so that we can GET (get the values of these variables) in the methods defined in the controller, and then we can have more control.
Okay, when this url reaches our controller, we receive the judgment and then turn on an isPost switch so that we can call the following methods. Think about turning on and off lights and computers. Switches are what we often do. .
Here we just changed the place.
OK. Let’s look at the following method for this switch.
We define an array, $results = array(); The role of this array is obvious. It will save any data we get from the model, and can also save the special parameters of GET from the url. Then it will be displayed in the template included in our require_once(*****) below, and the path is defined in the path variable.
At the same time we will receive 2 prompt parameters,
error means that there is an error in the operation. It is inevitable for everyone, including computers. Everyone will make mistakes. The key is to admit it. Computers do a good job and they have the courage to admit their mistakes.
status; indicates status, which is a successful operation.
$pagination = new Pagination; This class is our pagination class. We pass in a total number to it, and then it will calculate the total number of pages. Each time it jumps to a page, it is equivalent to refreshing once, so everyone The method is to GET (obtain) the value of the page on the url in the constructor, so that we know the current page. At the same time, we regenerated the query statement, followed by a limiting statement, similar to limit $start (starting id), $offset (length); the principle is to give me 10 records starting from this id; I The setting is 10, you can also be more flexible.
$cat = new Category; This class will be discussed in detail later, and it is also a very important classification model. Here we simply obtain all categories under this type and display them in the sidebar. I have completed it. There are pictures and the truth!
In this way, our $results array stores all the data needed for our page. Okay, let's take a look at how our template is output.
复制代码 代码如下: Arist's Blog Hinging there, everything will be fine. | |||||||||||||
分类
if( isset( $results['categories'] ) && ! empty( $results['categories'] ) ){ foreach( $results['categories'] as $category ){ echo << EOB; } } ?> | if( isset( $results['statusMessage'] )){echo $results['statusMessage'];} if( isset( $results['errorMessage'] )){echo $results['errorMessage'];} ?> 文章(主要用于转载,发布原创博文要通过“随笔”) if( isset( $results['posts'] )){ echo <<
if( isset( $pagination) ){$pagination->createLinks( ) ;} } else { echo "当前无内容!"; } ?> |
© Arist The above is just to show the data, everyone can do it. How do we operate this data? Operation is like a control ability. When I was playing football as a student, I had a strong ability to control the court. I won one championship, one runner-up, and one third place in college football games. I didn't go to senior year, and I also won numerous honors in middle school. My position is central defender. In this position on the football field, you must have the ability to see the overall situation, strong personal ability, and commanding ability. To put it too far, now you sit on the computer every day Before, these things were gone long ago, Just some words of experience left. But you must also experience the taste. My blog has a shortcoming. Every time you perform a read or write operation on the database, you have to refresh it! I know this places a heavy load on the server, but I feel that if you don’t fully understand a new technology and use it blindly, it will only be counterproductive. So for the time being, I will sacrifice the server’s response time and memory consumption to achieve relative stability! So I don’t know the overall situation very well, and there are still many unknown areas that I haven’t gotten involved in, such as in-depth ajax, in-depth php, c. . . No more to say. Okay, let’s see how to CRUD the data! DELETE Delete First look at this command post.php?action=delete&postID=132 When we confirm that we want to delete, there is something to note here. We can first perform a subtraction operation of 1 on the count_child_number field under the category to which the document belongs. So: when your grammar is correct, it may be that your logic is wrong! Or the method is wrong! That's my note! Please see:
Copy code The code is as follows: $post = new Post; $filter['post_id'] = isset( $_GET['postID'] ) ? ( int )$_GET['postID'] : "";// !important Before deleting data, reduce the number of articles in this category by 1 // Otherwise you don’t know the number of articles to delete in that category // I made a logical mistake. I deleted the document first, and then checked the category ID of the document; I could never find it because it no longer existed. $post->reduceChildNumber( "category", ( int ) $_GET['postID'] ); $result = $post->delete("post", $filter ); Here we can easily call the delete() method as long as we initialize the model at the top of our article. CREATE INSERT First look at this command post.php?action=newPost To be honest, I haven't inserted it in a long time. hehe! See the control method:Copy code The code is as follows:
So in the template, when we use isset() to make a judgment, we get a null value, which is a good thing; in this way, we will not output anything to the template. In this way, we wait for the user to submit the form. Here, to save trouble, I have not filtered the form for the time being, but I have left a backdoor to update it later. Okay, let's say our form is submitted (and filtered by your basic filter).
This is already half the battle. Below is insert***(). This is the benefit of mysql's universal database operation class, which can help you handle various forms and types. Of course, if you have more detailed requirements, you can inherit it and extend it
Look at the code: 到这里就差不多了,我们实现了几乎所有的基本操作。 几点说明,这些action 有的是导航,有的是生成的,大部分是固定的。自己看着用吧。 下篇说说 分类的事!还有就是这篇博客写完后会放在一个网站上 你如果想要源码学习的话,我会提供下载。谢谢你花这么长时间听我唠叨, 看到这句的人,祝你们 昨天 6,1 快乐,嘿嘿。
Related labels:
source:php.cn
Previous article:PHP Prevents JS Injection Regular_PHP Tutorial
Next article:How to set up anti-leeching in PHP_PHP tutorial
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
Latest Articles by Author
Latest Issues
Group MySQL results by ID for looping over
I have a table with flight data in mysql. I'm writing a php code that will group and displ...
From 2024-04-06 17:27:56
0
1
406
Related Topics
More>
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
|