脚本宝典收集整理的这篇文章主要介绍了thinkphp6-请求,脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
控制器 app/controller/Index.php
<?php
namespace appcontroller;
use thinkRequest;
class Index
{
PRotected $request;
public function __construct(Request $request)
{
$this->request = $request;
}
public function index()
{
return $this->request->param('name');
}
}
测试与结果
http://127.0.0.1:8000/index?name=huyongjian
huyongjian
控制器 app/controller/Index.php
<?php
namespace appcontroller;
use thinkRequest;
class Index
{
public function index(Request $request)
{
return $request->param('age');
}
}
测试与结果
http://127.0.0.1:8000/index?age=32
32
控制器 app/controller/Index.php
<?php
namespace appcontroller;
use thinkfaCADeRequest;
class Index
{
public function index()
{
return Request::param('username');
}
}
测试与结果
http://127.0.0.1:8000/index?username=huyongjian
huyongjian
控制器 app/controller/Index.php
<?php
namespace appcontroller;
class Index
{
public function index()
{
return request()->param('name');
}
}
测试与结果
http://127.0.0.1:8000/index?name=xiaoming
xiaoming
控制器
<?php
namespace appcontroller;
use thinkfacadeRequest;
class Index
{
public function index()
{
return Request::url();
}
}
测试与结果
http://127.0.0.1:8000/index?name=xiaoming
/index?name=xiaoming
支持获取的请求信息
方法 含义
host 当前访问域名或者IP
scheme 当前访问协议
port 当前访问的端口
remotePort 当前请求的REMOTE_PORT
protocol 当前请求的SERVER_PROTOCOL
contentTyPE 当前请求的CONTENT_TYPE
domain 当前包含协议的域名
subDomain 当前访问的子域名
panDomain 当前访问的泛域名
rootDomain 当前访问的根域名
url 当前完整URL
baseUrl 当前URL(不含QUERY_STRING)
query 当前请求的QUERY_STRING参数
baseFile 当前执行的文件
root URL访问根地址
rootUrl URL访问根目录
pathinfo 当前请求URL的pathinfo信息(含URL后缀)
ext 当前URL的访问后缀
time 获取当前请求的时间
type 当前请求的资源类型
method 当前请求类型
rule 当前请求的路由对象实例
控制器
Request::controller();
Request::controller(true);//小写
操作
Request::action();
Request::action(true);//小写
转小写
parse_name(Request::controller());
parse_name(Request::action());
控制器 app/controller/Index.php
<?php
namespace appcontroller;
use thinkfacadeRequest;
class Index
{
public function index()
{
//检测变量
$bool = Request::has('id', 'get');
var_dump($bool);
//获取变量
// 获取当前请求的name变量
$name = Request::param('name','XiaoMing');
VAR_dump($name);
}
}
测试与结果
http://127.0.0.1:8000/index?id=2&name=huyongjian
bool(true) string(10) "huyongjian"
全局过滤
Request类 app/Request.php
<?php
namespace app;
// 应用请求对象类
class Request extends thinkRequest
{
protected $filter = ['strip_tags'];
}
控制器 app/controller/Index.php
<?php
namespace appcontroller;
use appBaseController;
use thinkfacadeRequest;
class Index extends BaseController
{
public function index()
{
$str = Request::get('str');
var_dump($str);
}
}
测试与结果
http://127.0.0.1:8000/index?str=Hello%20%3Cb%3Eworld!%3C/b%3E
string(12) "Hello world!"
局部过滤
控制器 app/controller/Index.php
<?php
namespace appcontroller;
use thinkfacadeRequest;
class Index
{
public function index()
{
Request::filter(['strip_tags']);
$str = Request::get('str');
var_dump($str);
}
}
获取方法过滤
Request::get('name','','htMLspecialchars'); // 获取get变量 并用htmlspecialchars函数过滤
Request::param('username','','strip_tags'); // 获取param变量 并用strip_tags函数过滤
Request::post('name','','orgFilter::safeHtml'); // 获取post变量 并用OrgFilter类的safeHtml方法过滤
用途 方法
获取当前请求类型 method
判断是否GET请求 isGet
判断是否POST请求 isPost
判断是否PUT请求 isPut
判断是否DELETE请求 isDelete
判断是否AJAX请求 isAjax
判断是否pjax请求 isPjax
判断是否JSON请求 isJson
判断是否手机访问 ismobile
判断是否HEAD请求 isHead
判断是否PATCH请求 isPatch
判断是否OPTIONS请求 isOptions
判断是否为CLI执行 isCli
判断是否为CGI模式 isCgi
$info = Request::header();
echo $info['accept'];
echo $info['accept-encoding'];
echo $info['user-agent'];
// 定义GET请求路由规则 并设置3600秒的缓存
Route::get('new/:id','News/read')->cache(3600);
以上是脚本宝典为你收集整理的thinkphp6-请求全部内容,希望文章能够帮你解决thinkphp6-请求所遇到的问题。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。