接下来我会用OOP实现一个CMS网站(就简单做一下文章的管理展示功能,其他功能就不做了,也许这不能称为CMS系统)。
网站的文件组织:
classes文件夹用来存放相关的类,includes文件夹存放将被包含的页面,views文件夹存放用于网页展示的页面,css就是样式表文件所在地了。
1、创建数据库
CREATE DATABASE cms CHARACTER SET UTF8;
USE cms;
CREATE TABLE pages (
id INT UNSIGNED NOT NULL AUTO_INCREMENT,
craetorId INT UNSIGNED NOT NULL,
title VARCHAR(100) NOT NULL,
content TEXT NOT NULL,
dateUpdated TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
dateAdded TIMESTAMP NOT NULL,
PRIMARY KEY (id),
INDEX (craetorId),
INDEX (dateUpdated)
)CHARACTER SET UTF8;
注意建库建表时指定编码,以防中文乱码。
表结构如下:
2、制作网页模板
这里制作网页的头部模板和脚部模板,相关文件保存在includes文件夹下。
首先制作头部-header.inc.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title><?php echo (isset($pageTitle)) ? $pageTitle : '文章站点'; ?></title>
<link rel="stylesheet" href="css/index.css">
</head>
<body>
<header>
<h1>CONTENT SITE<br><span>home to lots of great contents!</span></h1>
<nav>
<ul>
<li><a href="index.php">首页</a></li>
<li><a href="####">存档</a></li>
<li><a href="contact.php">联系</a></li>
<li>
<?php
if (@$user) {
echo '<a href="logout.php">退出</a>';
}
else {
echo '<a href="login.php">登录</a>';
}
?>
</li>
<li><a href="register.php">注册</a></li>
</ul>
</nav>
</header>
接下来制作脚步-footer.inc.php
<footer>
<small>© <?php echo date('Y'); ?>- Website design by Kaivn</small>
</footer>
</body>
</html>
3、编写工具类文件
定义自动加载类的函数,并创建数据库连接-utilities.inc.php
<?php
//自动从classes文件夹下加载所需类
function class_loadeer($class) {
require('classes/' .$class. '.php');
}
spl_autoload_register('class_loadeer');
//创建数据库连接对象
try {
$pdo = new PDO('mysql:dbname=cms; host=localhost', 'root', '');
$pdo->query('set names utf8');
} catch (PDOException $e) {
$pageTitle = '错误!';
include('includes/header.inc.php');
include('views/error.html');
include('includes/footer.inc.php');
exit();
}
?>
4、创建Error页面:
该文件保存在views文件夹下-error.html:
<section>
<article>
<h1>发生错误!</h1>
<br>
<br>
<p>由于发生错误,内容无法加载。我们为此感到万分抱歉。相关错误已经反馈给管理员,我们会尽快处理。</p>
<br>
<p>错误详情:<span class="error"><?php echo $e->getMessage(); ?></span></p>
</article>
</section>
5、定义需要的类:
文件命名为:page.php,存放在classes文件夹下
<?php
class Page {
protected $id = null;
protected $creatorId = null;
protected $title = null;
protected $content = null;
protected $dateAdded = null;
protected $dateUpdated = null;
function getId() {
return $this->id;
}
function getCreatorId() {
return $this->creatorId;
}
function getTitle() {
return $this->title;
}
function getContent() {
return $this->content;
}
function getDateAdded() {
return $this->dateAdded;
}
function getDateUpdated() {
return $this->dateUpdated;
}
function getIntro($count = 400) {
return (substr($this->content, 0, $count)) . '...';
}
}
?>
6、创建主页:
index.php
<?php
include('includes/utilities.inc.php');
$pageTitle = '欢迎来到文章站点';
include('includes/header.inc.php');
try {
$q = 'SELECT id, title, content, date_format(dateUpdated, "%e %M %Y") AS dateUpdated From pages';
$r = $pdo->query($q);
if ($r) {
$r->setFetchMode(PDO::FETCH_CLASS, 'Page');
include('views/index.html');
}
else {
throw new Exception('现在无任何文章!');
}
} catch (Exception $e) {
include('views/error.html');
}
include('includes/footer.inc.php');
?>
注意这里有一行代码:
$r->setFetchMode(PDO::FETCH_CLASS, 'Page');
这行代码将从数据库读取的数据构造成Page类的对象,这样我们就可以使用定义的方法来操作数据了。
接下来是:views文件夹下的index.html
<section class="threeColumns">
<?php
while ($page = $r->fetch()) {
echo '<article>';
echo '<h1><span>'.$page->getDateUpdated().'</span><br><span>'.$page->getTitle().'</span></h1><br>';
echo '<p>'.$page->getIntro().'</p><br><br>';
echo '<p><a href="page.php?id='.$page->getId().'">阅读详情</a></p>';
echo '</article>';
}
?>
</section>
这里就是按照Page类的对象来操作数据。
制作阅读详情页的page.php
<?php
require('includes/utilities.inc.php');
try {
if (!isset($_GET['id']) || !filter_var($_GET['id'], FILTER_VALIDATE_INT, array('min_range' => 1))) {
throw new Exception('请提供有效的ID!');
}
$q = 'SELECT id, title, content, date_format(dateUpdated, "%e %M %Y") AS dateUpdated From pages WHERE id = :id';
$stmt = $pdo->prepare($q);
$r = $stmt->execute(array(':id' => $_GET['id']));
if ($r) {
$stmt->setFetchMode(PDO::FETCH_CLASS, 'Page');
$page = $stmt->fetch();
if ($page) {
$pageTitle = $page->getTitle();
include('includes/header.inc.php');
include('views/page.html');
}
else {
throw new Exception('请提供有效的ID!');
}
}
else {
throw new Exception('请提供有效的ID!');
}
} catch (Exception $e) {
$pageTitle = '错误!';
include('includes/header.inc.php');
include('views/error.html');
}
include('includes/footer.inc.php');
?>
接下来制作views文件夹下的page.html:
<section class="fullWidth">
<article>
<h1>
<span><?php echo $page->getDateUpdated(); ?></span>
<br>
<span><?php echo $page->getTitle(); ?></span>
</h1>
<br>
<p><?php echo $page->getContent(); ?></p>
</article>
</section>