怎么使用
有两种办法,一个是使用系统的request类,第二个是使用系统助手函数。
//下面使用助手函数
$res=request();
dump($res);
//下面先导入系统类,在实例化,由于request属于单例模式,所以,不能用new实例化。
<?php
namespace app\index\controller;
use think\Request;
class Index
{
public function index()
{
$res=Request::instance();
dump($res);
}
}
//这个是最常见的,这个赋值初始化,前提是,要继承Controller类,也要导入request类
<?php
namespace app\index\controller;
use think\Request;
use think\Controller;
class Index extends Controller
{
public function index(Request $res)
{
dump($res);
}
}
获取到request之后,最简单的是可以获取所有的请求
<?php
namespace app\index\controller;
use think\Request;
use think\Controller;
class Index extends Controller
{
public function index(Request $res)
{
echo 'domain: ' . $res->domain() . '<br/>';
dump($res);
}
}
获取变量
判断有没有这个参数
<?php
namespace app\index\controller;
use think\Request;
use think\Controller;
class Index extends Controller
{
public function index(Request $res)
{
//判断是不是有get请求id的变量
dump($res->has('id','get'));
//或者使用助手函数
dump(input('?get.id'));
}
}
读取所有的变量包括get和post
<?php
namespace app\index\controller;
use think\Request;
use think\Controller;
class Index extends Controller
{
public function index(Request $res)
{
// 获取当前请求的name变量
$res->param('name');
// 获取当前请求的所有变量(经过过滤)
$res->param();
// 获取当前请求的所有变量(原始数据)
$res->param(false);
// 获取当前请求的所有变量(包含上传文件)
$res->param(true);
input('param.name');
input('param.');
//或者
input('name');
input('');
}
}
读取get变量
<?php
namespace app\index\controller;
use think\Request;
use think\Controller;
class Index extends Controller
{
public function index(Request $res)
{
//读取get请求的id值
dump($res->get('id'));
//或者使用助手函数读取get请求的id值
dump(input('get.id'));
//读取get请求的所有的值
dump($res->get(''));
//或者使用助手函数读取get请求的所有的值
dump(input('get.'));
}
}
读取post变量
<?php
namespace app\index\controller;
use think\Request;
use think\Controller;
class Index extends Controller
{
public function index(Request $res)
{
//读取get请求的id值
dump($res->post('id'));
//或者使用助手函数读取get请求的id值
dump(input('post.id'));
//读取get请求的所有的值
dump($res->post(''));
//或者使用助手函数读取get请求的所有的值
dump(input('post.'));
}
}
这里有一个原始数据和过滤数据的概念
所有的数据都要过滤一下,过滤之后更安全
变量的过滤
过滤的目的是为了防止sql注入,为了安全
<?php
namespace app\index\controller;
use think\Request;
use think\Controller;
class Index extends Controller
{
public function index(Request $res)
{
//下面是过滤所有的数据
$res->filter(['strip_tags','htmlspecialchars']);
dump($res->param());
//还有其他方式过滤
$res->get('name','','htmlspecialchars'); // 获取get变量 并用htmlspecialchars函数过滤
$res->param('username','','strip_tags'); // 获取param变量 并用strip_tags函数过滤
$res->post('name','','org\Filter::safeHtml'); // 获取post变量 并用org\Filter类的safeHtml方法过滤
$res->param('username','','strip_tags,strtolower'); //多个过滤规则
}
}
还有数据排除
这里不编写了
基本就是获取里面添加only和except,用到了,就去手册里面看例子。
数据的修饰和转换
- 这里有两个,一个是强制改变类型,用修饰符来实现
input('get.id/d');//转换成整型
input('post.name/s');//转换成字符
input('post.ids/a');//转成数组
Request::instance()->get('id/b');//转换成布尔
- 另一个是替换掉发过来的请求。
//现在我是感觉没有什么打得用处????
// 更改GET变量
Request::instance()->get(['id'=>10]);
// 更改POST变量
Request::instance()->post(['name'=>'thinkphp']);
判断当前的请求类型
//这个方法不错可以直接利用这个做安全,让不是ajax访问的,不显示内容,不浪费资源了
if (Request::instance()->isGet()) echo "当前为 GET 请求";
// 是否为 POST 请求
if (Request::instance()->isPost()) echo "当前为 POST 请求";
// 是否为 Ajax 请求
if (Request::instance()->isAjax()) echo "当前为 Ajax 请求";
// 是否为手机访问
if (Request::instance()->isMobile()) echo "当前为手机访问";
//当然也可以用助手函数
if (request()->isGet()) echo "当前为 GET 请求";
也可以模拟请求
感觉不重要不写了
伪静态
这个直接用默认就行了,意思是,url生成的地址带后缀,html,可以直接使用
参数绑定
- 参数绑定的意思是直接在写方法时候在方法括号内就接受,参数
- 参数接受参数小于实际的参数
- 参数的名字必须和地址栏上面一一对应
- 可以设置默认值