- 开发无止境 -
Data: 2015-08-10 18:24:26Form: JournalClick: 6
think\facade\Request类负责
)调用序号 | 方法 | 说明 |
---|---|---|
1 | param | 获取当前请求的变量 |
2 | get | 获取 $_GET 变量 |
3 | post | 获取 $_POST 变量 |
4 | put | 获取 PUT 变量 |
5 | delete | 获取 DELETE 变量 |
6 | session | 获取 SESSION 变量 |
7 | cookie | 获取 $_COOKIE 变量 |
8 | request | 获取 $_REQUEST 变量 |
9 | server | 获取 $_SERVER 变量 |
10 | env | 获取 $_ENV 变量 |
11 | route | 获取 路由(包括PATHINFO) 变量 |
12 | middleware | 获取 中间件赋值/传递的变量 |
13 | file | 获取 $_FILES 变量 |
14 | filter | 变量过滤 |
15 | only | 获取部分变量 |
16 | except | 排除部分变量 |
PARAM
类型变量是框架提供的用于自动识别当前请求的一种变量获取方式,是系统推荐的获取请求参数的方法param
方法会把当前请求类型的参数和路由变量以及GET请求合并,并且路由变量是优先的controller代码
public function edit(){
print_r( $_GET ); // 原生get接收
print_r( Request::param() ); // 获取当前请求的所有变量
print_r( Request::param('id') ); // 获取当前请求的id变量
print_r( Request::get() );
}
view代码:index.html
<button class="layui-btn layui-btn-xs" onclick="edit({$right_v.id})">编辑</button>
<script type="text/javascript">
function edit(id){
layer.open({
type: 2,
title: '添加',
shade: 0.3,
area: ['480px', '440px'],
content: '/index.php/index/edit?id='+id
});
}
</script>
controller代码
public function edit(){
$id = Request::param('id');
$shop = Db::table('shop_goods')->where('id',$id)->find();
$cat = Db::table('shop_cat')->where('status',1)->select();
View::assign([
'shop' => $shop,
'cat' => $cat
]);
return View::fetch();
}
public function edits(){
// print_r( Request::param() );
// print_r( Request::post() );
$all = Request::param();
$update = Db::table('shop_goods')->where('id',$all['id'])->update($all);
if($update){
echo json_encode(['code'=>0,'msg'=>'修改成功']);
}else{
echo json_encode(['code'=>1,'msg'=>'修改失败']);
}
}
view代码:edit.html
<!DOCTYPE html>
<html>
<head>
<title></title>
<link rel="stylesheet" type="text/css" href="/static/layui/css/layui.css">
<script type="text/javascript" src="/static/layui/layui.js"></script>
</head>
<body style="padding:10px;">
<form class="layui-form">
<input type="hidden" name="id" value="{$shop.id}">
<div class="layui-form-item">
<label class="layui-form-label">标题</label>
<div class="layui-input-inline">
<input type="text" class="layui-input" name="title" value="{$shop.title}">
</div>
</div>
<div class="layui-form-item">
<label class="layui-form-label">分类</label>
<div class="layui-input-inline">
<select name="cat">
<option value=0 {if $shop['cat']==0} selected {/if}></option>
{volist name="cat" id="cat_v"}
<option value="{$cat_v['id']}" {if $shop['cat']==$cat_v['id']} selected {/if}>{$cat_v['name']}</option>
{/volist}
</select>
</div>
</div>
<div class="layui-form-item">
<label class="layui-form-label">原价</label>
<div class="layui-input-inline">
<input type="text" class="layui-input" name="price" value="{$shop.price}">
</div>
</div>
<div class="layui-form-item">
<label class="layui-form-label">折扣</label>
<div class="layui-input-inline">
<input type="text" class="layui-input" name="discount" value="{$shop.discount}">
</div>
</div>
<div class="layui-form-item">
<label class="layui-form-label">库存</label>
<div class="layui-input-inline">
<input type="text" class="layui-input" name="stock" value="{$shop.stock}">
</div>
</div>
<div class="layui-form-item">
<label class="layui-form-label">状态</label>
<div class="layui-input-inline">
<select name="status">
<option value="1" {if $shop['status']==1} selected {/if}>开启</option>
<option value="0" {if $shop['status']==0} selected {/if}>关闭</option>
</select>
</div>
</div>
</form>
<div class="layui-form-item">
<div class="layui-input-block">
<button class="layui-btn" onclick="save()">保存</button>
</div>
</div>
<script type="text/javascript">
layui.use(['layer','form'],function(){
form = layui.form;
layer = layui.layer;
$ = layui.jquery;
});
function save(){
$.post('/index.php/Index/edits',$('form').serialize(),function(res){
if(res.code>0){
layer.alert(res.msg,{icon:2});
}else{
layer.msg(res.msg);
setTimeout(function(){parent.window.location.reload();},1000);
}
},'json');
}
</script>
</body>
</html>
序号 | 修饰符 | 作用 |
---|---|---|
1 | s | 强制转换为字符串类型 |
2 | d | 强制转换为整型类型 |
3 | b | 强制转换为布尔类型 |
4 | a | 强制转换为数组类型 |
5 | f | 强制转换为浮点类型 |
Request::get('id/d');
Request::post('name/s');
Request::param('price/f');
序号 | 方法 | 说明 |
---|---|---|
1 | method | 获取当前请求类型 |
2 | has | 判断传值是否存在 |
3 | isGet | 判断是否GET请求 |
4 | isPost | 判断是否POST请求 |
5 | isPut | 判断是否PUT请求 |
6 | isDelete | 判断是否DELETE请求 |
7 | isAjax | 判断是否AJAX请求 |
8 | isPjax | 判断是否PJAX请求 |
9 | isJson | 判断是否JSON请求 |
10 | isMobile | 判断是否手机访问 |
11 | isHead | 判断是否HEAD请求 |
12 | isPatch | 判断是否PATCH请求 |
13 | isOptions | 判断是否OPTIONS请求 |
14 | isCli | 判断是否为CLI执行 |
15 | isCgi | 判断是否为CGI模式 |
public function edit(){
if(Request::method() == 'POST'){
// print_r(Request::method());exit;
$all = Request::param();
$update = Db::table('shop_goods')->where('id',$all['id'])->update($all);
if($update){
echo json_encode(['code'=>0,'msg'=>'修改成功']);
}else{
echo json_encode(['code'=>1,'msg'=>'修改失败']);
}
}else{
// print_r(Request::method());exit;
$id = Request::param('id');
$shop = Db::table('shop_goods')->where('id',$id)->find();
$cat = Db::table('shop_cat')->where('status',1)->select();
View::assign([
'shop' => $shop,
'cat' => $cat
]);
return View::fetch();
}
}
controller代码
public function add(){
if(Request::method() == 'POST'){
$all = Request::param();
$insert = Db::table('shop_goods')->insert($all);
if($insert){
echo json_encode(['code'=>0,'msg'=>'添加成功']);
}else{
echo json_encode(['code'=>1,'msg'=>'添加失败']);
}
}else{
$cat = Db::table('shop_cat')->where('status',1)->select();
View::assign([
'cat' => $cat
]);
return View::fetch();
}
}
view代码:edit.html