tp请求和响应

请求和响应

获得请求类

1.系统函数

<?php
namespace app\index\controller;
class Index
{
    public function index()
    {
        //使用系统方法
        $request=request();
        dump($request);
    }

}

2.使用request类

<?php
namespace app\index\controller;
use think\Request;
class Index
{
    public function index()
    {
        //使用系统request类
        //request属于单例模式,不能直接new
        $request=Request::instance();
    }
}

3.使用系统控制器类+request类

<?php
namespace app\index\controller;
use think\Request;
class Index
{
    //实例化Request类
    public function index(Request $request)
    {
        dump($request);
    }
}

获取URL请求

<?php
namespace app\index\controller;
use think\Request;
class Index
{
    //获取url信息
    public function getURL(Request $request)
    {
        //获得域名
        dump($request->domain());
        //获取url地址 (除了域名以外的部分)
        dump($request->url());
        //获得pathinfo路径
        dump($request->pathinfo());
    }
}

伪静态

将路径伪装成静态页面,让搜索引擎更好收录网站,防止别人知道网站的真实开发语言

获取请求模块/控制器/方法

<?php
namespace app\index\controller;
use think\Request;
class Index
{
    //获取url信息
    public function getURL(Request $request)
    {
       //当前模块
        dump($request->module());
        //当前控制器
        dump($request->controller());
        //当前方法
        dump($request->action());
    }
}

获取请求相关类型

<?php
namespace app\index\controller;
use think\Request;
class Index
{
    //获取请求类型
    public function getAsk(Request $request)
    {
       //请求类型
        dump($request->method());
        //资源类型
        dump($request->type());
        //访问地址
        dump($request->ip());
        //判断是不是ajax请求
        dump($request->isAjax());
        //获取地址栏参数
        dump($request->param());
    }
}

获取变量

以get为例

<?php
namespace app\index\controller;
use think\Request;
class Index
{
    //获取变量
    public function getVar(Request $request)
    {
       //判断get类型中的id是否存在 如果存在返回true
        dump($request->has('id','get'));
        dump(input('?get.id'));

        //读取参数
        dump($request->get('id'));
        dump(input('get.id'));

        //读取所有参数
        dump($request->get());
        dump(input('get.'));
    }
}

变量过滤

<?php
namespace app\index\controller;
use think\Request;
class Index
{
    public function type()
    {
       return view();
    }
    //过滤掉不合适的内容
    public function check(Request $request){
        //把传过来的内容转成实体
        //过滤一次
        $request->filter("htmlspecialchars");
        //多种方法过滤
        $request->filter(["htmlspecialchars","strip_tags"]);
        dump($request->post());

        //设置单个变量过滤再加密
        dump($request->post('name','','htmlspecialchars,md5'));
        
    }
}


变量的排除与排除

<?php
namespace app\index\controller;
use think\Request;
class Index
{
    public function type()
    {
       return view();
    }
    //过滤掉不合适的内容
    public function check(Request $request){
        //只获取一个变量
        dump($request->only('name'));
        //只排除一个变量
        dump($request->except('name'));
         //id强制转化成整型
        dump(input('get.id/d'));
        
    }
}

获取变量类型

s 字符串

d 整型

f 浮点型

a 数组

b 布尔型

<?php
namespace app\index\controller;
use think\Request;
class Index
{
    public function type()
    {
       return view();
    }
  
    public function check(Request $request){
   
         //id强制转化成整型
        dump(input('get.id/d'));
        
    }
}

更改变量

<?php
namespace app\index\controller;
use think\Request;
class Index
{
    public function type()
    {
       return view();
    }
    //更改变量
    public function check(Request $request)
    {
        dump($request->get('id'));
        dump($request->get(['id'=>30]));
        dump($request->get('id'));
        //访问http://tp.com/index.php/index/index/check.html?id=1111
        //结果 id=30
    }
}

判断请求类型

<?php
namespace app\index\controller;
use think\Request;
class Index
{
    public function type()
    {
       return view();
    }
   
    public function check(Request $request)
    {
   		 //判断是不是手机端
        dump($request->isMobile());
    }
}

模拟请求

application/index/view/index/type

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>$Title$</title>
</head>
<body>
    <form  method="post" action="{:url('check')}">
        user:<label for=""></label><input type="text" name="name" id="">
        <input type="hidden" name="_method" value="PUT">
        <br>
        word:<label>
        <input type="password" name="pass" >
        </label>
        <input type="submit" value="提交">
    </form>
</body>
</html>

application/index/controller/index

<?php
namespace app\index\controller;
use think\Request;
class Index
{
    public function type()
    {
       return view();
    }

    public function check(Request $request)
    {
        dump($request->method());
    }
}

伪静态

作用

1.保证SEO优化效果

2.为了网络的安全

设置

和对应的配置有关

在配置文件中修改

'url_html_suffix' => 'shtml'

获取当前的伪静态后缀

<?php
namespace app\index\controller;
use think\Request;
class Index
{
    public function type()
    {
       return view();
    }

    public function check(Request $request)
    {
        dump($request->ext());
    }
}

参数绑定

使用

<?php
namespace app\index\controller;
use think\Request;
class Index
{
    public function type()
    {
       return view();
    }
    //http://tp.com/index.php/index/index/check.html?id=1&name=qqq
    public function check($id,$name='admin')
    {
        dump($id);
        dump($name);
    }
}

注意

  • 参数绑定的个数,少于地址栏的参数的个数
  • 参数绑定的名字,必须和地址栏参数名字一一对应
  • 可以设置默认值
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值