演示框架的目的,我们按常规路线来搞:
如果直接访问
http://server/project/index.php
,框架默认调用 IndexAction(这
个 默 认 的 Action 是 可 以 配 置 的 , 详 情 参 见 Thinkphp 开 发 手 册 ) , 使 用
http://server/project/index.php/Action
的形式,默认访问该 Action 的 index 方法。
所有 Action、Model 以及模板文件都应该使用 UTF8 编码。我们在 Lib\Action 目
录下编写一个 IndexAction.class.php,内容如下:
<?php
class Index Action extends Action
{
function index()
{
echo “Hello world”;
}
}
?>
默认访问 IndexAction 的 index 方法,所以访问
http://server/project/index.php
和访问
http://server/project/index.php/Index/index
效果是一样的(Linux 区分文件
名大小写,所有应该确保 Action 名称大小写与文件名一致)。
页面输出 Hello world。
下面我们演示一下如何用模板输出变量。
在 Tpl 目录下新建 default 目录,这是默认的模板,多模板的话就建多个文
件夹。然后在 default 目录下新建 Index 目录,该目录名与 Action 对应。然后新建
index.html,该文件名默认与调用的方法一致。编写 index.html 内容如下:
<b>{$str}</b>
然后我们修改开始编写的 index 方法,内容如下:
function index()
{
$this->assign(“str”, “Hello world”);
$this->display();
}
访问
http://server/project/index.php
,页面源码为<b>Hello world</b>。
2 Thinkphp 的 CURD
CURD 需要涉及到数据库部分,而且模板显示内容一般也是跟数据库紧密
集合的。所以准备了一个非常简单的 BBS 的例子。
先看一下数据库结构。