TP5简单复习及快速上手1
TP5,一个国产的MVC框架
model,模型
view,视图
controller,控制器
控制器:
定义,大驼峰
跳转,$this->success(‘模块/控制器/方法’)、$this->error
重定向,$this->redirect(‘模块/控制器/方法’)
前置操作,
beforeActionList = [
‘first’ => [‘only’=>’second’’]
]
初始化操作
_initialize()
多级控制器:
controller/v1/Goods.php
url访问:tp5.com/admin/v1.Goods
接收请求参数:
$request = Request -> instance()实例化对象
$request -> get()
$request -> post()
$request -> param()
$request -> isGet()是否get请求
$request -> isPost()是否post请求
$request -> isAjax()是否isAjax请求
request('')
input(‘id’,’default’,’strip_tags’);
视图:
模板标签:tpl_begin => ‘<{’
模板变量:{$name}
内置的变量:
{$Think.get.name}
{$Think.post.name}
{$Think.session.name}
{$Think.server.name}
$_SERVER
比较标签:
{eq name='id' value="10"}等于{/eq}
gt、lt、egt、elt、neq
{empty name="age"} {/empty}
{defined name="APP_PATH"}{}
{if condition=""}
{elseif condition=""}
{else/}
{/if}
{switch}
循环标签:
{foreach $arr as $v}
{foreach name=”arr” item=”v” key=”index”}
{volist name=”arr” id=”vo” offset length mod key}
模板替换:
__CSS__
__STATIC__
__JS__
__URL__
通过配置文件:
view_replace_str => [
__CSS__ =>
]
加载资源:
{load href=”common.css,index.css”}
模板继承:
{block name=”content”}{/block}
渲染模板:
view(),函数
$this -> fetch()---常用
$this -> display(),不需要模板文件
模型:
连接数据库:database.php里面
对数据库操作:
• 原生SQL操作:
Db::query(“SELECT * FROM goods”);
Db::execute(“UPDATE goods SET goods_name=’’”)
• Db类查询构造器
• 查询
Db::table(‘goods’)-> where(‘goods_id’,’>’,5)->field(‘goods_name,shop_price’)
->alias()//别名
->group->limit->page->having()->order()->select()
插入一条记录
Db::table(‘goods’)->insert($data)
插入多条记录
Db::table(‘goods’)->insertAll($data)
Db::table(‘goods’)->insertGetId($data)
更新数据
Db::table(‘goods’)-> where(‘goods_id’,’=’,3)-> update()
更改一个字段值
Db::table(‘goods’)-> where(‘goods_id’,’=’,3)-> setField()
Db::table(‘goods’)-> where(‘goods_id’,’=’,3)-> setInc()
Db::table(‘goods’)-> where(‘goods_id’,’=’,3)-> setDec()
删除
Db::table(‘goods’)->delete(10)
• ORM
表映射到类
记录映射到对象
字段映射到属性
$obj = Db::get(2) Db是表
$obj -> goods_name;
new User;
model(‘User’)
增:
$user = new User;
$user -> data($data) -> save()
$user- > saveAll($data)
echo $user -> id;
删:
$user = new User;
$user –>delete()
$user -> destroy(“1,3,5”);
改:
$user = new User;
$user -> where(‘goods_id’,’=’,1)->save($data)
$user -> save($data,[‘id’=>3)
foreach(){
$user ->isUpdate(false)->save($data)
}
查:
User::get(2)
一条记录
User::where(‘goods_id’,’=’,3)->find()
多条记录
User::where(‘goods_id’,’=’,3)->limit->order->field()->select()
模型读取器:
get字段Attr() getImgAttr($value)
模型修改器:
set字段Attr() setPasswordAttr($value)
例如$model -> password = ‘admin123’使用就会自动加密
模型的自动完成
protected $auto
protected $insert = [‘insert_time’,’update_time’]
protected $update
public functiuon setInsertTimeAttr(){
return time();
}
模型自动写入时间戳
protected $autoWriteTimestamp = true
protected $createTime = ‘i_time’
protected $updateTime = ‘i_time’
软删除
user SoftDelete;
自动在删除的记录上修改delete_time的值
关联模型:
一对一:
protected functiuon profile(){
return $this -> hasOne(‘Progile’,’uid’,’id’)
}
一对多:
return $this -> hasMany(‘Comment’,’article_id’,’id’)
$article = new Article;
$article -> with(‘comment’) -> select();
//建议
$artivle -> delete(1);
$comment -> where(‘article’,’=’,1) ->delete()
helper.php
extend,扩展类库
在控制器中,Loader::import(‘类名’,EXTEND_PATH,’.class.php’)
new \
vendor,通过composer下载的第三方类库
验证器:
protected $rule = [
‘name’ => ‘require|length:5’
‘id’ => ‘require|isPositive’
]
protected $messafe = [
‘name’ => ‘’
]
protected $scene = [
‘edit’ => ‘id’
‘add’ => ‘’
]
protected function isPositive’(){
}
$validate = new UserValiudate()
$validate ->scence(‘edit’) -> batch() -> check($data)