4.2 Request:HTTP请求头 + 伪静态 + 参数绑定 + 请求缓存

一 HTTP请求头

//1 获取全部头信息
Request::header();//获取全部请求头信息
var_dump($info);
echo $info['accept'];//单独输出某个变量

//2 获取某一个头信息 equest::header('accept');//直接获取某个请求头信息

//3 请求头名称不区分大小写 _ 会转为 -
$agent = Request::header('user-agent');//正确
$agent = Request::header('USER_AGENT');//正确
array(11) {
  ["cookie"] => string(88) "thinkphp_show_page_trace=0|0; thinkphp_show_page_trace=0|0; thinkphp_show_page_trace=0|0"
  ["accept-language"] => string(14) "zh-CN,zh;q=0.9"
  ["accept-encoding"] => string(13) "gzip, deflate"
  ["accept"] => string(135) "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9"
  ["user-agent"] => string(114) "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36"
  ["upgrade-insecure-requests"] => string(1) "1"
  ["cache-control"] => string(9) "max-age=0"
  ["connection"] =>string(10) "keep-alive"
  ["host"] =>string(12) "www.blog.com"
  ["content-length"] =>string(0) ""
  ["content-type"] =>string(0) "

 

二 伪静态设置

1)匹配一种后缀

1 修改 config目录下route.php中配置项 'url_html_suffix' => 'html'

2)匹配任何后缀

'url_html_suffix' => '' 值设为空
http://serverName/blog/3.html  //可以访问
http://serverName/blog/3.shtml //可以访问
http://serverName/blog/3.xml   //可以访问
http://serverName/blog/3.pdf   //可以访问

3)匹配多种后缀

'url_html_suffix' => 'html|shtml' 多个值用|分隔
http://serverName/blog/3.html //可以访问
http://serverName/blog/3.shtm //可以访问
http://serverName/blog/3.xml  //不能访问
http://serverName/blog/3.pdf  //不能访问

4)关闭伪静态

'url_html_suffix' => false
关闭后 不再支持伪静态URL方式的访问,并且伪静态后缀将会被解析为最后一个参数的值

例如:http://serverName/blog/read/id/3.html
最终的id参数的值将会变成 3.html 。

5)获取伪静态的后缀

$ext = Request::ext();

 

三 参数绑定

1 参数绑定:就是把当前请求的变量作为 操作方法(也包括架构方法)的参数直接传入,参数绑定并不区分请求类型。

2 变量名绑定不一定由访问URL决定 路由地址也能起到相同的作用

3 多个参数顺序无所谓 只要变量名称对就可以
http://www.blog.com/admin/demo/demo?id=10&name=libai
http://www.blog.com/admin/demo/demo?name=libai&id=10
public function demo($id=0,$name='f'){
    echo $id;
    echo $name;
}

4 为了更好的配合前端规范,支持自动识别小写+下划线的请求变量使用驼峰注入
http://www.blog.com/admin/demo/demo?blog_id=4
public function demo($blogId=0){
    echo $blogId;
}

 

 四 请求缓存

支持对GET请求设置缓存访问,并设置有效期

学习了路由再来补充这一块

 

 

  

 

知识梳理

1、 多应用默认上传更目录: runtime/storage/目录下

2、 处理上传的主要类文件: think\filesystem\Driver.php

3、 上传的用到的方法: pubFile('保存路径',$file,'文件名规则');//文件名规则:支持 md5 和 sha1 默认是data

4、调整上传存储目录到 public 下

 

一 单文件上传

1)表单页面

<form action="" enctype="multipart/form-data" method="post">
    <input type="file" name="image" />
    <input type="submit" value="上传" />
</form>

2)控制器页面

<?php
namespace app\admin\controller;
use think\facade\Request;
use think\facade\Filesystem;

class Demo
{
    public function index(){

        //$file变量是一个 think\File对象
        $file = Request::file('image'); //1 获取表单上传文件

        //putFile()方法 上传到本地服务器
        //$saveName  = Filesystem::putFile('',$file);//返回文件保存的路径
        $saveName = Filesystem::disk('public')->putFile('',$file);//2 保存到public/storage目录下

        //$saveName打印结果:abc/20210111\b0174c6ee22b7a099ca7103f08a40a5d.png

        var_dump($saveName);

        return view('');
    }
}

 

二 多文件上传

1)表单页面

<form action="" enctype="multipart/form-data" method="post">
    <input type="file" name="image[]" /> <br><br>
    <input type="file" name="image[]" /> <br><br>
    <input type="submit" value="上传" />
</form>
input的name值是数组形式的

 

2)控制器页面

<?php
namespace app\admin\controller;
use think\facade\Filesystem;
use think\facade\Request;
class Demo
{
    public function index(){
        $files = Request::file('image'); //1 获取表单上传文件
        $saveName = [];
        foreach ($files as $file) {
            $saveName[] = Filesystem::putFile('',$file); //2 调用上传方法
        }
        var_dump($saveName); //3 得到需要保存的图片路径
        return view('');
    }
}

 

三 上传验证

1)基本验证

就是在上传前做文件大小后缀的验证
<?php namespace app\admin\controller; use think\facade\Filesystem; use think\facade\Request; class Demo { public function index(){ $files = Request::file('image'); try{ validate(['image'=>'filesize:10240|fileExt:jpg|image:200,200,jpg'])->check($files); $saveName = []; foreach ($files as $file) { $saveName[] = Filesystem::disk('public')->putFile('',$file); } } catch (\think\exception\ValidateException $e) { echo $e->getError(); } return view(''); } }

2)验证类型

fileSize   //文件大小

fileExt    //文件后缀,多个用逗号分割或者数组

fileMime   //文件MIME类型,多个用逗号分割或者数组

image      //验证图像文件的尺寸和类型

 3)获取文件的 hash 散列值

//支持md5 和 sha1

echo $file->md5();  //bf9f5888849ce2a031ed2c114f0e5785
echo $file->sha1(); //65f090b7bc94a7b85063deca05e4e61820cf9606

echo $file->hash('md5');
echo $file->hash('sha1');

 

四 配置文件

return [
    // 默认磁盘
    'default' => env('filesystem.driver', 'local'),
    // 磁盘列表
    'disks'   => [
        'local'  => [
            'type' => 'local',
            'root' => app()->getRuntimePath() . 'storage',
        ],
        'public' => [
            // 磁盘类型
            'type'       => 'local',
            // 磁盘路径
            'root'       => app()->getRootPath() . 'public/storage',
            // 磁盘路径对应的外部URL路径
            'url'        => '/storage',
            // 可见性
            'visibility' => 'public',
        ],
        // 更多的磁盘配置信息
    ],
];

 

posted @ 2021-01-11 15:30  棉花糖88  阅读(227)  评论(0)    收藏  举报