强哥ThinkPHP学习笔记

 

TP框架:
1.模板引擎
2.MVC设计模式
3.常用操作类

模板引擎和框架区别
1.模板引擎只是框架中用来做php和html分离

MVC设计模式
M model 数据模型
V view 视图
C control 控制器

V(html模板) -> C(PHP逻辑控制) -> M(Model类表操作)


定义一个新app:
define("APP_NAME","home");
define("APP_PATH","./home/");
define("APP_DEBUG",true);
include "ThinkPHP/ThinkPHP.php";
会生成home目录

模板定界符:
ThinkPHP/Conf/convention.php
TMPL_L_DELIM 模板引擎普通标签开始标记 {
TMPL_R_DELIM 模板引擎普通标签结束标记 }

与数据库有关的配置:
ThinkPHP/Conf/convention.php
'DB_TYPE' => 'mysql', // 数据库类型
'DB_HOST' => 'localhost', // 服务器地址
'DB_NAME' => 'test', // 数据库名
'DB_USER' => 'root', // 用户名
'DB_PWD' => '123456', // 密码
'DB_PORT' => '3306', // 端口
'DB_PREFIX' => '', // 数据库表前缀

url/入口/模块/操作


ThinkPHP支持四种URL模式,可以通过设置URL_MODEL参数来定义,包括
1.普通模式
http://localhost/test/index.php?m=Index&&a=index
2.PATHINFO
http://localhost/test/index.php/Index/index
3.REWRITE
http://localhost/test/Index/index
4.兼容模式
http://localhost/test/index.php/?s=/Index/index

REWRITE方式来访问模式和操作
1.apache必须支持mod_rewrite.so模块
2.htaccess里面书写的rewrite模式

常量:
__ROOT__ 网站根目录地址
__APP__ 当前项目(入口文件)地址
__GROUP__ 当前分组的URL地址
__URL__ 当前模块的URL地址
__ACTION__ 当前操作的URL地址
__SELF__ 当前URL地址
__INFO__ 当前的PATH_INFO字符串
__EXT__ 当前URL地址的扩展名

模板替换:
../Public: 会被替换成当前项目的公共模板目录 通常是 /项目目录/Tpl/当前主题/Public/
__TMPL__: 会替换成项目的模板目录 通常是 /项目目录/Tpl/当前主题/
(注:为了部署安全考虑,../Public和__TMPL__不再建议使用)
__PUBLIC__:会被替换成当前网站的公共目录 通常是 /Public/
__ROOT__: 会替换成当前网站的地址(不含域名)
__APP__: 会替换成当前项目的URL地址 (不含域名)
__GROUP__:会替换成当前分组的URL地址 (不含域名)
__URL__: 会替换成当前模块的URL地址(不含域名)
__ACTION__:会替换成当前操作的URL地址 (不含域名)
__SELF__: 会替换成当前的页面URL


配置:
Home/Conf/config.php
'DEFAULT_MODULE' => 'Index', //默认模块

读取配置:
C();

Home/Common/common.php
写到里面的函数可以在本应用中所有模块的操作中使用

包含扩展类:
import("ORG.Util.Image");
ThinkPHP/Extend/Library/ORG/Util/Image.class.php

登录验证:

//权限认证
class CommonAction extends Action{
function _initialize(){
echo "先进行权限把控!";
}
}
//登录后
class IndexAction extends CommonAction{}
//登录页
class LoginAction extends Action{}

REWRITE方式:
1.apache必须支持mod_rewrite.so模块
LoadModule rewrite_module modules/mod_rewrite.so
2.网站根目录支持解析rewrite重写表达式文件.htaccess
3..htaccess里面书写的rewrite表达式

生成地址必须用U()方法

1."URL_MODEL" => "2"
0 http://localhost/test/index.php?m=Index&&a=index
1 http://localhost/test/index.php/Index/index
2 http://localhost/test/Index/index
3 http://localhost/test/index.php/?s=/Index/index
2.链接做成rewrite
<a href="{:U("show","","html")}">aaa</a>

在模板中使用PHP函数,要在函数前面加冒号

异常模板:
Home/Conf/conf.php
'TMPL_EXCEPTION_FILE' => APP_PATH.'/Public/exception.tpl'

异常模板中科院使用的异常变量:
$e['file'] 异常文件名
$e['line'] 异常发生的文件行数
$e['message'] 异常信息
$e['trace'] 异常的详细信息

操作中获取当前地址中的模板和操作:
1.$_GET['_URL_'][0]
$_GET['_URL_'][1]
2.MODULE_NAME
ACTION_NAME

空操作:
public function _empty(){}
空模块:
class EmptyAction extends Action{}

伪静态:
'URL_HTML_SUFFIX'=>'shtml'

url重定向:
$this -> redirect();

页面跳转
$this -> success("success",U("show"));

url路由:
'URL_ROUTER_ON' => true, //开启路由
'URL_ROUTE_RULES' => array( //定义路由规则
'news/:year/:month/:day' => array('News/archive', 'status=1'),
'news/:id' => 'News/read',
'news/read/:id' => '/news/:1',
),

url大小写:
'URL_CASE_INSENSITIVE' =>true

跨模块调用:
$test = A("Test");
$test->index();
A("项目中的模块");

$test=R("Test/index");
R("模块中的方法");

注:aa"bb
1.addslashes变成aa\"bb
2.htmlspecialchars变成aa\&quot;bb
3.mysql_escape_string变成aa\\&quot;bb
"

修改Dbmysql.class.php
public function escapeString($str) {
if (get_magic_quotes_gpc()){
return $str;
}
if($this->_linkID) {
return mysql_real_escape_string($str,$this->_linkID);
}else{
return mysql_escape_string($str);
}
}

M方法:
$user=new Model('User');
$user=M("User");

D方法:
$user = new UserModel('User');
$user = D("User");
//如果UserModel.class.php不存在,则使用model类

$model -> getLastSql() //获取model操作的最后一条sql语句

protected $tableName = "user";//设置model类在组合sql时真实的表名

$user -> getPk(); //获取主键

$model-> create();
//生成model对象中的数据对象data,可以智能过滤post中与字段不相符的下标
//自动验证
//自动完成

字段映射:
class UserModel extends Model{
protected $_map = array(
"name" => "username",
"pass" => "password"
);
}
//注意后面只能用D()和create()

自动完成:

protected $_auto=array(
array('password','md5',3,'function'),
);

CURD操作:
insert:
1.$model->add($_POST);
2.$model->create();
$model->add();
update:(save)
$user->create()
$user->save()
//
$user->save($_POST)
find:find
$user -> find($id);
$user -> where("id={$id}") -> find();
select:
$user -> select();
$user -> select($id);
$user -> where("id={$id}") -> select();
delete:
$user -> delete($id);
$user -> where("id={$id}") -> delete();
$user -> where(array('id'=>$id)) -> delete();

左连接:
原始:
select user.username,score.num from user left join score on user.id=score.uid;
TP:
$user -> join(' score ON user.id = score.uid') -> select();


模板注释:
{// 单行注释}
{/*多行注释 */}

快捷缓存(数据缓存)
默认缓存时间0
if(!S('rows')){
$user = M('User');
$rows = $user -> select();
S('rows',$rows);
}
$this -> rows = S('rows');

静态缓存(模板缓存)
HTML_CACHE_ON 是否开启静态缓存功能
HTML_FILE_SUFFIX 静态文件后缀 惯例配置的值是 .html
HTML_CACHE_TIME 默认的静态缓存有效期 默认60秒 可以在静态规则定义覆盖
'HTML_CACHE_ON'=>true,
'HTML_CACHE_RULES'=> array(
'ActionName' => array('静态规则', '静态缓存有效期', '附加规则'),
'ModuleName(小写)' => array('静态规则', '静态缓存有效期', '附加规则'),
'ModuleName(小写):ActionName' => array('静态规则', '静态缓存有效期', '附加规则'),
'*' => array('静态规则', '静态缓存有效期', '附加规则'),
//…更多操作的静态规则
)


删除session:
session(null);
session('[destroy]');
cookie('name',null);


图片缩放:
import('ORG.Util.Image');
$img = new Image();
$path = './Public/Uploads/Images/';
$src = $path.'a.jpg';
$dst = $path.'w_a.jpg';
$img->thumb($src,$dst,'',50,50);


1._initialize()方法;

TP的类中存在_initialize方法的话,在调用本类中的任何一个方法前都会先调用执行_initialize方法的代码。

2.杂项:
session支持
cookie
数据分页
文件上传
验证码
图片水印
ip定位

RBAC权限系统:
1.人
2.模块
3.方法

user表
role角色表
group表
module表
action表


//数组引用
$arr=array(1,2,3);
foreach($arr as &$val){
$val++;
}
print_r($arr);

posted @ 2017-06-23 14:45  Redheat  阅读(228)  评论(0编辑  收藏  举报