欢迎光临
感谢一路有你

tp5+小程序开发笔记(六)

如果你对该文章中的内容有疑问/不解,可以点击此处链接提问
要注明问题和此文章链接地址 点击此处跳转
 

在application/extra下的配置文件都会被tp5自动加载

新建setting.php

return[
 'img_prefix'=>'http://wmc.com/images'
]

 

使用模型的获取器拼接图片路径

tp5中只有public是公开的

public/images/1,jpg

config('配置文件.配置名称');
$a = config('setting.img_prefix);
数据库中的图片地址   /1.jpg

model
获取器,当时用时自动调用
函数名:  get字段名Attr
public function getUrlAttr($value,$data){
 if($data['type'] == 1){
//本地的图片
    $a = $value;// /1.jpg 
    return config('setting.img_prefix').$value;
 }else{
//网络完整的地址
    return $value;
 }  
}

api版本

开闭原则

对扩展开放,对修改封闭(版本的替换)

if($version == 1){

}
if($version == 2){

}
路由
v1:
Route::get('api/v1/banner/:id','api/v1.banner/getBanner');
升级版:
Route::get('api/:version/banner/:id','api/:version.banner/getBanner');

完整匹配路由

config.php

'route_complete_match'=>true,

数据集对象

通过数据集临时隐藏字段

1.自己设置数据集

$products =  ProductModel::aaa($a);
$collection = collection($products);
$products = $collection->hidden(['summary']);//临时隐藏summary

2.自动设置数据集

database.php

'resultset_type'=>'collection'   //原生是array

$products = ProductModel::aaa($a); 
$products = $products->hidden(['summary']);//临时隐藏summary

如果查不到不报错返回空数组,使用如下
if($result->isEmpty()){
........
}

接口粒度与接口分层

 

 

 

 

 

赞(0) 打赏
未经允许不得转载:王明昌博客 » tp5+小程序开发笔记(六)
分享到: 更多 (0)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏