ThinkPHP 基本注意事项
1. 缓存
修改模版后要及时删除缓存,否则不能生效。
2. 控制器
2.1 变量赋值
注意变量用的'',而非$
$this->assign('str',"Hello ThinkPHP!");
而不是:
$this->assign($str,"Hello ThinkPHP!");
2.2 模版路径
$this->display('default/Index/index'); 不要加后缀(.html或其它),否则报以下错误
模板不存在[./Tpl/default/Index/index.html.html]
2.3 重定向
$this->redirect("index");
2.4 模型对应表
如 $Form = D("Demo");,对应的是 DB_PREFIX.demo表
2.5 成功与错误
$this->success("数据写入成功"); 定义了这个,就需要创建 ./Tpl/default/Public/success.html 模版
$this->error("数据写入失败");
header("Content-Type:text/html; charset=utf-8");
exit($Form->getError().' [ <A HREF="javascript:history.back()">返 回</A> ]');
主要变量:{$msgTitle} {$message} {$error} {$waitSecond} {$jumpUrl}
3. 模版
3.1 变量显示
{$str}
3.2 form post
表单form的method需指定为post,本人曾因未指定这个而出现莫名错误
3.3 present 标签
<present name="变量名">....</present>判断变量是否定义 <notpresent>
4. 常用变量、常量
. 网站根目录地址
:当前项目(入口文件)地址
__URL__ : <form method="post" action="__URL__/insert">
__PUBLIC__ : 网站公共目录,而非主题下的目录 <link href='__PUBLIC__/Css/common.css'>
__CURRENT__ : 当前模块的模板目录
APP_PATH : 当前项目目录
LIB_PATH : 项目类库目录
LANG_PATH : 项目语言文件目录
COMMON_PATH : 项目公共文件目录
APP_PUBLIC_PATH :项目公共文件目录
APP_TMPL_PATH : 项目模板目录
TEMPLATE_PATH :当前模版路径
WEB_PUBLIC_PATH :网站公共目录
5. 常用函数
<php>highlight_file(LIB_PATH.'Action/IndexAction.class.php');</php> 高亮显示文件 这是PHP语言的函数
6. 常用代码片段
6.1 IndexAction.class.php
<?php class IndexAction extends Action { public function index() { $Form = D( "Form" ); $list = $Form ->select(); $this ->assign( 'list' , $list ); $this ->display( './Tpl/default/Index/index' ); } public function insert() { $Form = new Model( "Form" ); if ( $Form ->Create()){ if (false!== $Form ->add()){ $this ->success( "数据写入成功" ); } else { $this ->error( "数据写入失败" ); } } else { header( "Content-Type:text/html; charset=utf-8" ); exit ( $Form ->getError(). ' [ <A HREF="javascript:history.back()" mce_HREF="javascript:history.back()">返 回</A> ]' ); } } } ?>6.2 FormModel.class.php
<?php class FormModel extends Model { // 自动验证设置 protected $_validate = array ( array ( 'title' , 'require' , '标题必须!' ,1), array ( 'email' , 'email' , '邮箱格式错误!' ,2), array ( 'content' , 'require' , '内容必须' ), array ( 'title' , '' , '标题已经存在' ,0, 'unique' ,self::MODEL_INSERT), ); // 自动填充设置 protected $_auto = array ( array ( 'status' , '1' ,self::MODEL_INSERT), array ( 'create_time' , 'time' ,self::MODEL_INSERT, 'function' ), ); } ?>查看更多关于ThinkPHP 基本注意事项 - Thinkphp的详细内容...