thinkphp 笔记 未完
2016-03-24 21:45
561 查看
如果打开admin.php 即可访问到Application/Admin 下的 IndexController
//默认加载admin模块
$_GET['m']='Admin';
//默认加载控制器
$_GET['c']='Index';
// 定义应用目录
define('APP_PATH','./Application/');
public function test($user,$password){
echo "user:$user<br/>password:$password";
}
域名:http://localhost/tp/Admin/User/test/user/wangmutian/password/123456
http://localhost/tp/index.php?m=Admin&c=user&a=test&user=wangmutian&password=123456
结果显示
user:wangmutian
password:123456
$user=new Model("User");
'SHOW_PAGE_TRACE'=>true
tinkphp 调试工具
M 执行的是 Model 本身,优点效率高
D('user') 执行的是 UserModel,如果home 模块下没有的模型类 就会在公共模块下找,如果都没有就会在Model 里找,如果想调用其他目录下的Model 就 D('Admin/User')
使用原身:
$user=M();
var_dump($user->query("select * from tp_user"));
第6课
1.查询方式
字符串条件查询 (不推荐使用)
$user=M('user');
var_dump($user->where("id=1")->select());
索引数组条件查询
$user=M('user');
$option["id"]=1;
$option["_logic"]="OR"; //修改默认连接符为OR
var_dump($user->where($option)->select());
对象数组查询
$user=M('user');
$option=new \stdClass(); //php5 提供的空类
$option->id=1;
var_dump($user->where($option)->select());
2.表达式查询
第7课
快捷查询:
$user=M("User");
$map['user|email']="蜡笔小新" (如果想把 or 换成 and 则把| 换成 &)
var_dump($user->where($map)->select());
select * from usesr where user='蜡笔小新' or email ='蜡笔小新'
$map['id&user']=>array(1,"蜡笔小新","_multi"=>true) 让他们可以一一对应
$map["id"]=>array(array('gt',1),array('lt',4)); id >1 AND id<4 区间查询如果想把and 换成 or 则在第三个参数上 加上 or,如: $map["id"]=>array(array('gt',1),array('lt',4),"or");
第8课
sql 连贯操作
$user=M("User");
$user->where("id>1")->order(" id desc ")->limit(2)->select();
或者
$user->select(array('where'=>array("id=1"),'order'=>'id desc','limit'=>2));
->find() //仅仅能查询到一个数据
->delete()
$user->field('id,name')->select() 查询出来的字段
$user->field('SUM(id) as count ')->select() 计算id 和 然后起一个别名
table 方法 多表切换
第9课
alias 用户数据表别名
group ->group(" id")
$user->join('think_user on think_info.id=think_user.id')->select();
$user->join('think_user on think_info.id=think_user.id','RIGHT')->select(); //右关联
第12~14课 未看
第15课 视图
//修改模板目录
//'VIEW_PATH'=>'./Public/'
//设置默认主题
'DEFAULT_THEME'=>'default'
如果想使用另一个主题在Controller中
$this->theme('blue')->display();
T() 获取当前模板地址
第16课
修改定界符
'TMPL_L_DELIM'=>'<{',
'TMPL_R_DELIM'=>'}>'
{$data['user']|mb_substr=0,3,'UTF-8'}
{$date|date='Y-H-d H:i:s'} 默认显示方式 是 date($date,'Y-H-d H:i:s') 这样是不对的,所以要修改成,{$date|date='Y-H-d H:i:s',###} 将参数传到最后
如果觉得以上方法太过于麻烦了,可以使用以下方法
{:mb_substr($date['user'],0,3,'UTF-8')}
{$user|default='没有数据'} //可以显示默认没有数据
第17课
三元运算符
{$user?'':''}
<include file='Public/header' />
<include file='Public/footer' />
模板布局
模板继承
index.html
<extend name='Public/base' />
<meta charset="UTF-8">
<block name="title">这是我要做的标题</block>
<block name="main">
这是我做的网页
</block>
base.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title><block name="title">Insert title here</block></title>
</head>
<body>
<include file='Public/header' />
<block name="main"></block>
<include file='Public/footer' />
</body>
</html>
模板布局
在config.php 中添加
'LAYOUT_ON'=>true,
'LAYOUT_NAME'=>'Public/layout'
View/public/layout.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<include file='Public/header' />
{__CONTENT__}
<include file='Public/footer' />
</body>
</html>
在其他页面上添加东西就可以显示出来了 如果不希望载入模板文件的,在第一行填写{__NOLAYOUT__}
标签方式
<layout name='Public/layout'/>
模板替换
__Root__ 会替换当前网站网址 不带域名
__APP__
__SELF__
__Public__ 会替换当前网站的公共目录 通常是/public/
第18课 内置标签
if 标签是支持 “=” ,其余都不支持 ,比如 “>”,"<" neq != elt <= egt >= lt < gt >
<if condition="$user eq '蜡笔小新'" >
<else if condition='$user eq '和其医护'>
<else />
</if>
switch 这个标签里头的name 是不需要写 $
<switch name="uesr">
<case value='蜡笔小新'></case>
<default />错误
</switch>
eq标签里头的那么也是不需要 写$
<eq name='user' value='蜡笔小小新'></eq>
<in name='id' value='1,2,3'></in>
<notin name='id' value='1,2,3'></notin>
<between name='id' value='1,10'></between>
第19课
<volist name='data' id='arr' offset='0' length='5' key='k'> 从第一条开始显示显示五条 k 会显示 123456。。。
{$k}{$arr.name} // 默认写 {$i} 就是写那个 k
</volist>
在模板中写变量
<assign name='var' value='123' />
{$var}
<define name='MY_NAME' value='456' />
{$Think.const.MY_NAME}
<import type='js' file='js.index' /> 默认路径是在/Public/
<import type='css' file='css.index' />
/Public/js/index.js
<import type='js' file='js.index' basepath="./Common"/> 修改默认路径
<load href="__PBULIC__/js/index.js" />
<js href=""/>
<css href="" />
第20课
如果要使用Html.class.php的扩展标签,那么要首先导入
<taglib name="html" /> 要在页面的最顶部使用
使用例如
<html:select options="name" selected="value">
另一种方法是
在config.php 中填写
'TAGLIB_BUILD_IN'=>'cx,html'
写入不需要加前缀
<select options="name" selected="value">
ps.设置内置标签,有一定的危险性,因为可能出现标签重复导致冲突
制作自己的标签
Thinkphp/library/Think/template/taglib/Mt.class.php
<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2014 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: wangmutian <wangmutian@gmail.com>
// +----------------------------------------------------------------------
namespace Think\Template\TagLib;
use Think\Template\TagLib;
defined("THINK_PATH") or die();
/**
* Mt标签库制作
*/
class Mt extends TagLib{
protected $tags=array(
'mytest'=>array('attr'=>'color,border','close'=>1)
);
public function _mytest($tag,$content){
$color='';
$border='';
if(isset($tag['color'])){
$color='color:'.$tag['color'].';';
}
if(isset($tag['border'])){
$border='border:'.$tag["border"].'px solid #000;';
}
$css=$color.$border;
return "<div style='{$css}'>".$content."</div>";
}
}
在页面的顶部 写<taglib name="Mt" />
写入标签为<Mt:mytest color="#f00" border="2">这个是我制作的标签那</Mt:mytest>
第21课 设置默认模块
//设置默认模块
'MODULE_ALLOW_LIST'=>array('Home','Admin'),
'DEFAULT_MODULE'=>'Home',
未完
第22课
//URL可以区分大小写
'URL_CASE_INSENSITIVE'=>true
数据缓存
设置存储时间的缓存方法 使用 S()
if(!$list=S('user')){
$list=M("user")->select();
S('user',$list,10);
}
$this->assign("list",$list);
$this->assign("user","蜡笔小新");
$this->display();
设置bu存储时间的缓存方法 使用 F()
查询缓存
$list=M("user")->cache(true)->select();
$list=M("user")->cache('cache_user',10)->select(); 自己起一个名字,然后设置成十秒钟
获取缓存的方法 是 var_dump(S('cache_user'));
删除缓存,S('cache_user',null);
sql解析缓存
config.php
'DB_SQL_BUILD_CACHE'=>true, (基本用的很少)
'HTML_CACHE_ON' => true, // 开启静态缓存
'HTML_CACHE_TIME' => 60, // 全局静态缓存有效期(秒)
'HTML_FILE_SUFFIX' => '.html', // 设置静态缓存文件后缀
'HTML_CACHE_RULES' => array( // 定义静态缓存规则
'Index:model'=>'123'
)
'HTML_CACHE_ON' => true, // 开启静态缓存
'HTML_CACHE_TIME' => 60, // 全局静态缓存有效期(秒)
'HTML_FILE_SUFFIX' => '.html', // 设置静态缓存文件后缀
'HTML_CACHE_RULES' => array( // 定义静态缓存规则
'Index:model'=>array('{:module}_{:controller}_{:action}_{id}',60)
//是以目录形式存储的
'Index:model'=>array('{:module}/{:controller}/{:action}/{id}',60)
//方法名
'index'
'User:index'
'User:'
'*'=>array('{$_SERVER.REQUEST_URI|md5}')
)
查询错误sql
echo $user->getLastSql();
echo $user->getDbError(); //输出错误sql语句
性能调试
G('begin');
G('end');
echo G('begin','end').'s'; //执行这段代码花了多长时间
echo G('begin','end').'kb' //输出内存开销
//默认加载admin模块
$_GET['m']='Admin';
//默认加载控制器
$_GET['c']='Index';
// 定义应用目录
define('APP_PATH','./Application/');
public function test($user,$password){
echo "user:$user<br/>password:$password";
}
域名:http://localhost/tp/Admin/User/test/user/wangmutian/password/123456
http://localhost/tp/index.php?m=Admin&c=user&a=test&user=wangmutian&password=123456
结果显示
user:wangmutian
password:123456
$user=new Model("User");
'SHOW_PAGE_TRACE'=>true
tinkphp 调试工具
M 执行的是 Model 本身,优点效率高
D('user') 执行的是 UserModel,如果home 模块下没有的模型类 就会在公共模块下找,如果都没有就会在Model 里找,如果想调用其他目录下的Model 就 D('Admin/User')
使用原身:
$user=M();
var_dump($user->query("select * from tp_user"));
第6课
1.查询方式
字符串条件查询 (不推荐使用)
$user=M('user');
var_dump($user->where("id=1")->select());
索引数组条件查询
$user=M('user');
$option["id"]=1;
$option["_logic"]="OR"; //修改默认连接符为OR
var_dump($user->where($option)->select());
对象数组查询
$user=M('user');
$option=new \stdClass(); //php5 提供的空类
$option->id=1;
var_dump($user->where($option)->select());
2.表达式查询
第7课
快捷查询:
$user=M("User");
$map['user|email']="蜡笔小新" (如果想把 or 换成 and 则把| 换成 &)
var_dump($user->where($map)->select());
select * from usesr where user='蜡笔小新' or email ='蜡笔小新'
$map['id&user']=>array(1,"蜡笔小新","_multi"=>true) 让他们可以一一对应
$map["id"]=>array(array('gt',1),array('lt',4)); id >1 AND id<4 区间查询如果想把and 换成 or 则在第三个参数上 加上 or,如: $map["id"]=>array(array('gt',1),array('lt',4),"or");
第8课
sql 连贯操作
$user=M("User");
$user->where("id>1")->order(" id desc ")->limit(2)->select();
或者
$user->select(array('where'=>array("id=1"),'order'=>'id desc','limit'=>2));
->find() //仅仅能查询到一个数据
->delete()
$user->field('id,name')->select() 查询出来的字段
$user->field('SUM(id) as count ')->select() 计算id 和 然后起一个别名
table 方法 多表切换
第9课
alias 用户数据表别名
group ->group(" id")
$user->join('think_user on think_info.id=think_user.id')->select();
$user->join('think_user on think_info.id=think_user.id','RIGHT')->select(); //右关联
第12~14课 未看
第15课 视图
//修改模板目录
//'VIEW_PATH'=>'./Public/'
//设置默认主题
'DEFAULT_THEME'=>'default'
如果想使用另一个主题在Controller中
$this->theme('blue')->display();
T() 获取当前模板地址
第16课
修改定界符
'TMPL_L_DELIM'=>'<{',
'TMPL_R_DELIM'=>'}>'
{$data['user']|mb_substr=0,3,'UTF-8'}
{$date|date='Y-H-d H:i:s'} 默认显示方式 是 date($date,'Y-H-d H:i:s') 这样是不对的,所以要修改成,{$date|date='Y-H-d H:i:s',###} 将参数传到最后
如果觉得以上方法太过于麻烦了,可以使用以下方法
{:mb_substr($date['user'],0,3,'UTF-8')}
{$user|default='没有数据'} //可以显示默认没有数据
第17课
三元运算符
{$user?'':''}
<include file='Public/header' />
<include file='Public/footer' />
模板布局
模板继承
index.html
<extend name='Public/base' />
<meta charset="UTF-8">
<block name="title">这是我要做的标题</block>
<block name="main">
这是我做的网页
</block>
base.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title><block name="title">Insert title here</block></title>
</head>
<body>
<include file='Public/header' />
<block name="main"></block>
<include file='Public/footer' />
</body>
</html>
模板布局
在config.php 中添加
'LAYOUT_ON'=>true,
'LAYOUT_NAME'=>'Public/layout'
View/public/layout.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<include file='Public/header' />
{__CONTENT__}
<include file='Public/footer' />
</body>
</html>
在其他页面上添加东西就可以显示出来了 如果不希望载入模板文件的,在第一行填写{__NOLAYOUT__}
标签方式
<layout name='Public/layout'/>
模板替换
__Root__ 会替换当前网站网址 不带域名
__APP__
__SELF__
__Public__ 会替换当前网站的公共目录 通常是/public/
第18课 内置标签
if 标签是支持 “=” ,其余都不支持 ,比如 “>”,"<" neq != elt <= egt >= lt < gt >
<if condition="$user eq '蜡笔小新'" >
<else if condition='$user eq '和其医护'>
<else />
</if>
switch 这个标签里头的name 是不需要写 $
<switch name="uesr">
<case value='蜡笔小新'></case>
<default />错误
</switch>
eq标签里头的那么也是不需要 写$
<eq name='user' value='蜡笔小小新'></eq>
<in name='id' value='1,2,3'></in>
<notin name='id' value='1,2,3'></notin>
<between name='id' value='1,10'></between>
第19课
<volist name='data' id='arr' offset='0' length='5' key='k'> 从第一条开始显示显示五条 k 会显示 123456。。。
{$k}{$arr.name} // 默认写 {$i} 就是写那个 k
</volist>
在模板中写变量
<assign name='var' value='123' />
{$var}
<define name='MY_NAME' value='456' />
{$Think.const.MY_NAME}
<import type='js' file='js.index' /> 默认路径是在/Public/
<import type='css' file='css.index' />
/Public/js/index.js
<import type='js' file='js.index' basepath="./Common"/> 修改默认路径
<load href="__PBULIC__/js/index.js" />
<js href=""/>
<css href="" />
第20课
如果要使用Html.class.php的扩展标签,那么要首先导入
<taglib name="html" /> 要在页面的最顶部使用
使用例如
<html:select options="name" selected="value">
另一种方法是
在config.php 中填写
'TAGLIB_BUILD_IN'=>'cx,html'
写入不需要加前缀
<select options="name" selected="value">
ps.设置内置标签,有一定的危险性,因为可能出现标签重复导致冲突
制作自己的标签
Thinkphp/library/Think/template/taglib/Mt.class.php
<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2014 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: wangmutian <wangmutian@gmail.com>
// +----------------------------------------------------------------------
namespace Think\Template\TagLib;
use Think\Template\TagLib;
defined("THINK_PATH") or die();
/**
* Mt标签库制作
*/
class Mt extends TagLib{
protected $tags=array(
'mytest'=>array('attr'=>'color,border','close'=>1)
);
public function _mytest($tag,$content){
$color='';
$border='';
if(isset($tag['color'])){
$color='color:'.$tag['color'].';';
}
if(isset($tag['border'])){
$border='border:'.$tag["border"].'px solid #000;';
}
$css=$color.$border;
return "<div style='{$css}'>".$content."</div>";
}
}
在页面的顶部 写<taglib name="Mt" />
写入标签为<Mt:mytest color="#f00" border="2">这个是我制作的标签那</Mt:mytest>
第21课 设置默认模块
//设置默认模块
'MODULE_ALLOW_LIST'=>array('Home','Admin'),
'DEFAULT_MODULE'=>'Home',
未完
第22课
//URL可以区分大小写
'URL_CASE_INSENSITIVE'=>true
数据缓存
设置存储时间的缓存方法 使用 S()
if(!$list=S('user')){
$list=M("user")->select();
S('user',$list,10);
}
$this->assign("list",$list);
$this->assign("user","蜡笔小新");
$this->display();
设置bu存储时间的缓存方法 使用 F()
查询缓存
$list=M("user")->cache(true)->select();
$list=M("user")->cache('cache_user',10)->select(); 自己起一个名字,然后设置成十秒钟
获取缓存的方法 是 var_dump(S('cache_user'));
删除缓存,S('cache_user',null);
sql解析缓存
config.php
'DB_SQL_BUILD_CACHE'=>true, (基本用的很少)
'HTML_CACHE_ON' => true, // 开启静态缓存
'HTML_CACHE_TIME' => 60, // 全局静态缓存有效期(秒)
'HTML_FILE_SUFFIX' => '.html', // 设置静态缓存文件后缀
'HTML_CACHE_RULES' => array( // 定义静态缓存规则
'Index:model'=>'123'
)
'HTML_CACHE_ON' => true, // 开启静态缓存
'HTML_CACHE_TIME' => 60, // 全局静态缓存有效期(秒)
'HTML_FILE_SUFFIX' => '.html', // 设置静态缓存文件后缀
'HTML_CACHE_RULES' => array( // 定义静态缓存规则
'Index:model'=>array('{:module}_{:controller}_{:action}_{id}',60)
//是以目录形式存储的
'Index:model'=>array('{:module}/{:controller}/{:action}/{id}',60)
//方法名
'index'
'User:index'
'User:'
'*'=>array('{$_SERVER.REQUEST_URI|md5}')
)
查询错误sql
echo $user->getLastSql();
echo $user->getDbError(); //输出错误sql语句
性能调试
G('begin');
G('end');
echo G('begin','end').'s'; //执行这段代码花了多长时间
echo G('begin','end').'kb' //输出内存开销
相关文章推荐
- 自定义PHP错误报告处理方式
- yii笔记
- 简单的PHP+Mysql实现分页
- php函数
- php基础
- PHP错误报告
- php加密函数
- ThinkPHP的I方法使用详解
- PHP实用方法
- PHP之PDO
- yii2-搜索带分页,分页的两种方式
- YII2.0 验证表单
- excel导入mysql(用PHP-ExcelReader 方法),日期时间出错的处理办法
- PHP学习(四)---PHP与数据库MySql
- Yii2.0 插入多条记录操作中,旧的属性值影响插入操作的原因
- PHP底层的运行机制与原理
- FragmentPagerAdapter与FragmentStatePagerAdapter
- php 处理高精度计算函数
- 【2014】【辛星】【php】【秋季】【2】第一个php程序
- window下yaf的下载选择