TP5学习(五):请求

一、请求信息

如果要获取当前的请求信息,可以使用\think\Request类,

除了

$request = Request::instance();

也可以使用助手函数

$request = request();

当然,最方便的还是使用注入请求对象的方式来获取变量。

获取URL信息

$request = Request::instance();
// 获取当前域名
echo 'domain: ' . $request->domain() . '<br/>';
// 获取当前入口文件
echo 'file: ' . $request->baseFile() . '<br/>';
// 获取当前URL地址 不含域名
echo 'url: ' . $request->url() . '<br/>';
// 获取包含域名的完整URL地址
echo 'url with domain: ' . $request->url(true) . '<br/>';
// 获取当前URL地址 不含QUERY_STRING
echo 'url without query: ' . $request->baseUrl() . '<br/>';
// 获取URL访问的ROOT地址
echo 'root:' . $request->root() . '<br/>';
// 获取URL访问的ROOT地址
echo 'root with domain: ' . $request->root(true) . '<br/>';
// 获取URL地址中的PATH_INFO信息
echo 'pathinfo: ' . $request->pathinfo() . '<br/>';
// 获取URL地址中的PATH_INFO信息 不含后缀
echo 'pathinfo: ' . $request->path() . '<br/>';
// 获取URL地址中的后缀信息
echo 'ext: ' . $request->ext() . '<br/>';

输出结果为:

domain: http://tp5.com
file: /index.php
url: /index/index/hello.html?name=thinkphp
url with domain: http://tp5.com/index/index/hello.html?name=thinkphp
url without query: /index/index/hello.html
root:
root with domain: http://tp5.com
pathinfo: index/index/hello.html
pathinfo: index/index/hello
ext: html

设置/获取 模块/控制器/操作名称

$request = Request::instance();
echo "当前模块名称是" . $request->module();
echo "当前控制器名称是" . $request->controller();
echo "当前操作名称是" . $request->action();

如果当前访问的地址是 http://serverName/index.php/index/hello_world/index

输出结果为:

当前模块名称是index
当前控制器名称是HelloWorld
当前操作名称是index

设置模块名称值需要向module方法中传入名称即可,同样使用于设置控制器名称和操作名称

Request::instance()->module('module_name');

获取请求参数

$request = Request::instance();
echo '请求方法:' . $request->method() . '<br/>';
echo '资源类型:' . $request->type() . '<br/>';
echo '访问ip地址:' . $request->ip() . '<br/>';
echo '是否AJax请求:' . var_export($request->isAjax(), true) . '<br/>';
echo '请求参数:';
dump($request->param());
echo '请求参数:仅包含name';
dump($request->only(['name']));
echo '请求参数:排除name';
dump($request->except(['name']));

输出结果为:

请求方法:GET
资源类型:html
访问ip地址:127.0.0.1
是否Ajax请求:false
请求参数:
array (size=2)
  'test' => string 'ddd' (length=3)
  'name' => string 'thinkphp' (length=8)
  
请求参数:仅包含name
array (size=1)
  'name' => string 'thinkphp' (length=8)
  
请求参数:排除name
array (size=1)
  'test' => string 'ddd' (length=3)

获取路由和调度信息

hello方法修改如下:

$request = Request::instance();
echo '路由信息:';
dump($request->route());
echo '调度信息:';
dump($request->dispatch());

路由定义为:

return [
    'hello/:name' =>['index/hello',[],['name'=>'\w+']],
];

访问下面的URL地址:

http://serverName/hello/thinkphp

输出信息为:

路由信息:
array (size=4)
  'rule' => string 'hello/:name' (length=11)
  'route' => string 'index/hello' (length=11)
  'pattern' => 
    array (size=1)
      'name' => string '\w+' (length=3)
  'option' => 
    array (size=0)
      empty
      
调度信息:
array (size=2)
  'type' => string 'module' (length=6)
  'module' => 
    array (size=3)
      0 => null
      1 => string 'index' (length=5)
      2 => string 'hello' (length=5)
      

设置请求信息

如果某些环境下面获取的请求信息有误,可以手动设置这些信息参数,使用下面的方式:

$request = Request::instance();
$request->root('index.php');
$request->pathinfo('index/index/hello');

二、输入变量

概述

可以通过Request对象完成全局输入变量的检测、获取和安全过滤,支持包括$_GET、$_POST、$_REQUEST、$_SERVER、$_SESSION、$_COOKIE、$_ENV等系统变量,以及文件上传信息。

检测变量是否设置

可以使用has方法来检测一个变量参数是否设置,如下:

Request::instance()->has('id','get');
Request::instance()->has('name','post');

或者使用助手函数

input('?get.id');
input('?post.name');

变量检测可以支持所有支持的系统变量。

变量获取

变量获取使用\think\Request类的如下方法及参数:

变量类型方法(‘变量名/变量修饰符’,‘默认值’,‘过滤方法’)

变量类型方法包括:

在这里插入图片描述
获取param变量

param变量是框架提供的用于自动识别GET、POST或者PUT请求的一种变量获取方式,是系统推荐的获取请求参数的方法,用法如下:

// 获取当前请求的name变量
Request::instance()->param('name');
// 获取当前请求的所有变量(经过过滤)
Request::instance()->param();
// 获取当前请求的所有变量(原始数据)
Request::instance()->param(false);
// 获取当前请求的所有变量(包含上传文件)
Request::instance()->param(true);

param方法会把当前请求类型的参数和PATH_INFO变量以及GET请求合并。

使用助手函数实现:

input('param.name');
input('param.');
或者
input('name');
input('');

因为input函数默认就采用PARAM变量读取方式。

获取GET变量

Request::instance()->get('id'); // 获取某个get变量
Request::instance()->get('name'); // 获取get变量
Request::instance()->get(); // 获取所有的get变量(经过过滤的数组)
Request::instance()->get(false); // 获取所有的get变量(原始数组)

注:pathinfo地址参数不能通过get方法获取,查看“获取PARAM变量”

获取POST变量

Request::instance()->post('name'); // 获取某个post变量
Request::instance()->post(); // 获取经过过滤的全部post变量
Request::instance()->post(false); // 获取全部的post原始变量

使用助手函数实现:

input('post.name');
input('post.');

获取PUT变量

Request::instance()->put('name'); // 获取某个put变量
Request::instance()->put(); // 获取全部的put变量(经过过滤)
Request::instance()->put(false); // 获取全部的put原始变量

使用助手函数实现:

input('put.name');
input('put.');

获取REQUEST变量

Request::instance()->request('id'); // 获取某个request变量
Request::instance()->request(); // 获取全部的request变量(经过过滤)
Request::instance()->request(false); // 获取全部的request原始变量数据

使用助手函数实现:

input('request.id');
input('request.');

获取SERVER变量

Request::instance()->server('PHP_SELF'); // 获取某个server变量
Request::instance()->server(); // 获取全部的server变量

使用助手函数实现:

input('server.PHP_SELF');
input('server.');

获取SESSION变量

Request::instance()->session('user_id'); // 获取某个session变量
Request::instance()->session(); // 获取全部的session变量

使用助手函数实现:

input('session.user_id');
input('session.');

获取Cookie变量

Request::instance()->cookie('user_id'); // 获取某个cookie变量
Request::instance()->cookie(); // 获取全部的cookie变量

使用助手函数实现:

input('cookie.user_id');
input('cookie.');

变量过滤

框架默认没有设置任何过滤规则,你可以是配置文件中设置全局的过滤规则:

// 默认全局过滤方法 用逗号分隔多个
'default_filter'         => 'htmlspecialchars',

也支持使用Request对象进行全局变量的获取过滤,过滤方式包括函数、方法过滤,以及PHP内置的Types of filters,我们可以设置全局变量过滤方法,例如:

Request::instance()->filter('htmlspecialchars');

支持设置多个过滤方法,例如:

Request::instance()->filter(['strip_tags','htmlspecialchars']),

也可以在获取变量的时候添加过滤方法,例如:

Request::instance()->get('name','','htmlspecialchars'); // 获取get变量 并用htmlspecialchars函数过滤
Request::instance()->param('username','','strip_tags'); // 获取param变量 并用strip_tags函数过滤
Request::instance()->post('name','','org\Filter::safeHtml'); // 获取post变量 并用org\Filter类的safeHtml方法过滤

可以支持传入多个过滤规则,例如:

Request::instance()->param('username','','strip_tags,strtolower'); // 获取param变量 并依次调用strip_tags、strtolower函数过滤

Request对象还支持PHP内置提供的Filter ID过滤,例如:

Request::instance()->post('email','',FILTER_VALIDATE_EMAIL);

框架对FilterID做了转换支持,因此也可以使用字符串的方式,例如:

Request::instance()->post('email','','email');

采用字符串方式定义FilterID的时候,系统会自动进行一次filter_id调用转换成Filter常量。

具体的字符串根据filter_list函数的返回值来定义。

需要注意的是,采用Filter ID 进行过滤的话,如果不符合过滤要求的话 会返回false,因此你需要配合默认值来确保最终的值符合你的规范。

例如,

Request::instance()->post('email','',FILTER_VALIDATE_EMAIL);

就表示,如果不是规范的email地址的话 返回空字符串。

如果当前不需要进行任何过滤的话,可以使用(V5.0.3+版本)

// 获取get变量 并且不进行任何过滤 即使设置了全局过滤
Request::instance()->get('name','',null); 

获取部分变量

如果你只需要获取当前请求的部分参数,可以使用:

// 只获取当前请求的id和name变量
Request::instance()->only('id,name');

或者使用数组方式

// 只获取当前请求的id和name变量
Request::instance()->only(['id','name']);

默认获取的是当前请求参数,如果需要获取其它类型的参数,可以使用第二个参数,例如:

// 只获取GET请求的id和name变量
Request::instance()->only(['id','name'],'get');
// 只获取POST请求的id和name变量
Request::instance()->only(['id','name'],'post');

排除部分变量

也支持排除某些变量获取,例如

// 排除id和name变量
Request::instance()->except('id,name');

或者使用数组方式

// 排除id和name变量
Request::instance()->except(['id','name']);

同样支持指定变量类型获取:

// 排除GET请求的id和name变量
Request::instance()->except(['id','name'],'get');
// 排除POST请求的id和name变量
Request::instance()->except(['id','name'],'post');

变量修饰符

input函数支持对变量使用修饰符功能,可以更好的过滤变量。

用法如下:

input(‘变量类型.变量名/修饰符’);

或者

Request::instance()->变量类型(‘变量名/修饰符’);

例如:

input('get.id/d');
input('post.name/s');
input('post.ids/a');
Request::instance()->get('id/d');

TP5版本默认的变量修饰符是/s,如果需要传入字符串之外的变量可以使用下面的修饰符,包括:
在这里插入图片描述

如果要获取的数据为数组,请一定注意要加上 /a 修饰符才能正确获取到。

三、更改变量

如果需要更改请求变量的值,可以通过下面的方式:

// 更改GET变量
Request::instance()->get(['id'=>10]);
// 更改POST变量
Request::instance()->post(['name'=>'thinkphp']);

尽量避免直接修改$_GET 或者 $_POST数据,同时也不能直接修改param变量,例如下面的操作是无效的:

// 更改请求变量
Request::instance()->param(['id'=>10])

四、请求类型

获取请求类型

在很多情况下面,需要判断当前操作的请求类型是GET、POST、PUT、DELETE或者HEAD,一方面可以针对请求类型作出不同的逻辑处理,另外一方面有些情况下面需要验证安全性,过滤不安全的请求。

TP5 取消了用于判断请求类型的系统常量(如IS_GET,IS_POST等),统一采用 think\Request类 处理请求类型。

用法如下

// 是否为 GET 请求
if (Request::instance()->isGet()) echo "当前为 GET 请求";
// 是否为 POST 请求
if (Request::instance()->isPost()) echo "当前为 POST 请求";
// 是否为 PUT 请求
if (Request::instance()->isPut()) echo "当前为 PUT 请求";
// 是否为 DELETE 请求
if (Request::instance()->isDelete()) echo "当前为 DELETE 请求";
// 是否为 Ajax 请求
if (Request::instance()->isAjax()) echo "当前为 Ajax 请求";
// 是否为 Pjax 请求
if (Request::instance()->isPjax()) echo "当前为 Pjax 请求";
// 是否为手机访问
if (Request::instance()->isMobile()) echo "当前为手机访问";
// 是否为 HEAD 请求
if (Request::instance()->isHead()) echo "当前为 HEAD 请求";
// 是否为 Patch 请求
if (Request::instance()->isPatch()) echo "当前为 PATCH 请求";
// 是否为 OPTIONS 请求
if (Request::instance()->isOptions()) echo "当前为 OPTIONS 请求";
// 是否为 cli
if (Request::instance()->isCli()) echo "当前为 cli";
// 是否为 cgi
if (Request::instance()->isCgi()) echo "当前为 cgi";

助手函数

// 是否为 GET 请求
if (request()->isGet()) echo "当前为 GET 请求";
……

五、请求伪装

请求类型伪装

支持请求类型伪装,可以在POST表单里面提交_method变量,传入需要伪装的请求类型,例如:

<form method="post" action="">
    <input type="text" name="name" value="Hello">
    <input type="hidden" name="_method" value="PUT" >
    <input type="submit" value="提交">
</form>

提交后的请求类型会被系统识别为PUT请求。

可以设置为任何合法的请求类型,包括GET、POST、PUT和DELETE等。

如果需要改变伪装请求的变量名,可以修改应用配置文件:

// 表单请求类型伪装变量
'var_method'             => '_m',

AJAX/PJAX伪装

可以对请求进行AJAX请求伪装,如下:

http://localhost/index?_ajax=1 

或者PJAX请求伪装

http://localhost/index?_pjax=1 

如果需要改变伪装请求的变量名,可以修改应用配置文件:

// 表单ajax伪装变量
'var_ajax'               => '_a',
// 表单pjax伪装变量
'var_pjax'               => '_p',

_ajax和_pjax可以通过GET/POST/PUT等请求变量伪装。

六、HTTP头信息

可以使用Request对象的header方法获取当前请求的HTTP 请求头信息,例如:

$info = Request::instance()->header();
echo $info['accept'];
echo $info['accept-encoding'];
echo $info['user-agent'];

也可以直接获取某个请求头信息,例如:

$agent = Request::instance()->header('user-agent');

HTTP请求头信息的名称不区分大小写,并且_会自动转换为-,所以下面的写法都是等效的:

$agent = Request::instance()->header('user-agent');
$agent = Request::instance()->header('User-Agent');
$agent = Request::instance()->header('USER_AGENT');

七、伪静态

URL伪静态通常是为了满足更好的SEO效果,ThinkPHP支持伪静态URL设置,可以通过设置url_html_suffix参数随意在URL的最后增加你想要的静态后缀,而不会影响当前操作的正常执行。例如

'url_html_suffix' => 'shtml'

我们可以把下面的URL http://serverName/Home/Blog/read/id/1 变成 http://serverName/Home/Blog/read/id/1.shtml

后者更具有静态页面的URL特征,但是具有和前面的URL相同的执行效果,并且不会影响原来参数的使用。

默认情况下,伪静态的设置为html,如果我们设置伪静态后缀为空字符串,

'url_html_suffix'=>''

则支持所有的静态后缀访问,如果要获取当前的伪静态后缀,可以使用Request对象的ext方法。

例如:

http://serverName/index/blog/3.html
http://serverName/index/blog/3.shtml
http://serverName/index/blog/3.xml
http://serverName/index/blog/3.pdf

都可以正常访问。

我们可以在控制器的操作方法中获取当前访问的伪静态后缀,例如:

$ext = Request::instance()->ext();

如果希望支持多个伪静态后缀,可以直接设置如下:

// 多个伪静态后缀设置 用|分割
'url_html_suffix' => 'html|shtml|xml' 

那么,当访问 http://serverName/Home/blog/3.pdf 的时候会报系统错误。

如果要关闭伪静态访问,可以设置

// 关闭伪静态后缀访问
'url_html_suffix' => false,

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

http://serverName/index/blog/read/id/3.html

最终的id参数的值将会变成 3.html。

八、方法注入

如果需要在Request请求对象中添加自己的方法,可以使用Request对象的方法注入功能,例如:

// 通过hook方法注入动态方法
Request::hook('user','getUserInfo');

getUserInfo函数定义如下

function getUserInfo(Request $request, $userId)
{
    // 根据$userId获取用户信息
    return $info;
}

接下来,我们可以直接在控制器中使用:

public function index()
{
    $info = Request::instance()->user($userId);
}

九、属性注入

可以动态注入当前Request对象的属性,方法:

// 动态绑定属性
Request::instance()->bind('user',new User);
// 或者使用
Request::instance()->user = new User;

获取绑定的属性使用下面的方式:

Request::instance()->user;

如果控制器注入请求对象的话,也可以直接使用

$this->request->user;

或者使用助手函数:

request()->user;

十、参数绑定

方法参数绑定是把URL地址(或者路由地址)中的变量作为操作方法的参数直接传入。

操作方法参数绑定

按名称绑定

参数绑定方式默认是按照变量名进行绑定,例如,我们给Blog控制器定义了两个操作方法read和archive方法,由于read操作需要指定一个id参数,archive方法需要指定年份(year)和月份(month)两个参数,那么我们可以如下定义:

namespace app\index\Controller;

class Blog 
{
    public function read($id)
    {
        return 'id='.$id;
    }

    public function archive($year='2016',$month='01')
    {
        return 'year='.$year.'&month='.$month;
    }
}

URL的访问地址分别是:

http://serverName/index.php/index/blog/read/id/5
http://serverName/index.php/index/blog/archive/year/2016/month/06

两个URL地址中的id参数和year和month参数会自动和read操作方法以及archive操作方法的同名参数绑定。

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

输出的结果依次是:

id=5
year=2016&month=06

按照变量名进行参数绑定的参数必须和URL中传入的变量名称一致,但是参数顺序不需要一致。也就是说

http://serverName/index.php/index/blog/archive/month/06/year/2016

和上面的访问结果是一致的,URL中的参数顺序和操作方法中的参数顺序都可以随意调整,关键是确保参数名称一致即可。

如果用户访问的URL地址是(至于为什么会这么访问暂且不提):

http://serverName/index.php/index/blog/read/

那么会抛出下面的异常提示: 参数错误:id

报错的原因很简单,因为在执行read操作方法的时候,id参数是必须传入参数的,但是方法无法从URL地址中获取正确的id参数信息。由于我们不能相信用户的任何输入,因此建议你给read方法的id参数添加默认值,例如:

public function read($id=0)
{
    return 'id='.$id;
}

这样,访问 http://serverName/index.php/index/blog/read/ 的时候 就会输出

id=0

始终给操作方法的参数定义默认值是一个避免报错的好办法

按顺序绑定

还可以支持按照URL的参数顺序进行绑定的方式,合理规划URL参数的顺序绑定对简化URL地址可以起到一定的帮助。

还是上面的例子,控制器不变,还是使用:

namespace app\index\Controller;

class Blog 
{
    public function read($id)
    {
        return 'id='.$id;
    }

    public function archive($year='2016',$month='01')
    {
        return 'year='.$year.'&month='.$month;
    }
}

我们在配置文件中添加配置参数如下:

// URL参数方式改成顺序解析
'url_param_type'         => 1,

接下来,访问下面的URL地址:

http://serverName/index.php/index/blog/read/5
http://serverName/index.php/index/blog/archive/2016/06

输出的结果依次是:

id=5
year=2016&month=06

按参数顺序绑定的话,参数的顺序不能随意调整,如果访问:

http://serverName/index.php/index/blog/archive/06/2016

最后的输出结果则变成:

id=5
year=06&month=2016

按顺序绑定参数的话,操作方法的参数只能使用URL pathinfo变量,而不能使用get或者post变量。

参数绑定有一个特例,如果你的操作方法中定义有Request对象作为参数的话,无论参数位置在哪里,都会自动注入,而不需要进行参数绑定。

架构方法参数绑

可以对架构函数进行参数绑定,当前请求的路由变量可以自动绑定到架构函数的参数,例如:

namespace app\index\Controller;

class Blog 
{
	protected $name;
	public function __construct($name = null)
    {
    	$this->name = $name;
    }
}

如果访问

http://localhost/index/index/index/name/thinkphp

当前请求的路由变量name的值thinkphp会自动传入架构方法的name变量。

十一、依赖注入

TP的依赖注入(也称之为控制反转)是一种较为轻量的实现,无需任何的配置,并且主要针对访问控制器进行依赖注入。可以在控制器的构造函数或者操作方法(指访问请求的方法)中类型声明任何(对象类型)依赖,这些依赖会被自动解析并注入到控制器实例或方法中。

自动注入请求对象

架构方法注入

在控制器的架构方法中会自动注入当前请求对象,例如:

namespace app\index\controller;

use think\Request;

class Index
{
	protected $request;
    
	public function __construct(Request $request)
    {
    	$this->request = $request;
    }
    
    public function hello()
    {
        return 'Hello,' . $this->request->param('name') . '!';
    }
    
}

操作方法注入

控制器的操作方法中如果需要调用请求对象Request的话,可以在方法中定义Request类型的参数,并且参数顺序无关,例如:

namespace app\index\controller;

use think\Request;

class Index
{

    public function hello(Request $request)
    {
        return 'Hello,' . $request->param('name') . '!';
    }
    
}

访问URL地址的时候 无需传入request参数,系统会自动注入当前的Request对象实例到该参数。

如果继承了系统的Controller类的话,也可以直接调用request属性,例如:

<?php
namespace app\index\controller;

use think\Controller;

class Index extends Controller
{

    public function hello()
    {
        return 'Hello,'.$this->request->param('name');
    }
    
}

其它对象自动注入

控制器的架构方法和操作方法支持任意对象的自动注入。

架构方法注入

namespace app\index\controller;

use app\index\model\User;
use think\Request;

class Index
{
	protected $request;
    protected $user;
    
	public function __construct(Request $request, User $user)
    {
    	$this->request = $request;
        $this->user = $user;
    }
    
}

对于已经进行了绑定(属性注入)的对象,即可自动完成依赖注入,如果没有进行对象绑定的话,会自动实例化一个新的对象示例传入(如果类定义有instance方法,则会自动调用instance方法进行实例化)。

架构方法的依赖注入不影响其它类型的参数绑定。

操作方法注入

把User模型绑定到当前请求对象:

Request::instance()->bind('user', \app\index\model\User::get(1));

然后就可以在操作方法中进行对象参数的自动注入,代码:

<?php
namespace app\index\controller;

use app\index\model\User;
use think\Controller;

class Index extends Controller
{

    public function hello(User $user)
    {
        return 'Hello,'.$user->name;
    }
    
}

如果没有事先在Request对象中进行对象绑定的话,调用hello方法的时候user参数会自动实例化,相当于完成了下面的绑定操作:

Request::instance()->bind('user', new \app\index\model\User);

对象自动注入不影响原来的参数绑定。

invoke方法自动调用

如果依赖注入的类有定义一个可调用的静态invoke方法,则会自动调用invoke方法完成依赖注入的自动实例化。

invoke方法的参数是当前请求对象实例,例如:

namespace app\index\model;

use think\Model;
class User extends Model
{
	public static function invoke(Request $request)
    {
    	$id = $request->param('id');
        return User::get($id);
    }
}

十二、请求缓存

请求缓存仅对GET请求有效,有两种方式可以设置请求缓存:

路由参数

可以在路由规则里面定义cache参数开启当前路由规则的请求缓存,例如:

// 定义GET请求路由规则 并设置3600秒的缓存
Route::get('new/:id','News/read',['cache'=>3600]);

第二次访问相同的路由地址的时候,会自动获取请求缓存的数据响应输出,并发送304状态码。

默认请求缓存的标识为当前访问的pathinfo地址,可以定义请求缓存的标识,如下:

// 定义GET请求路由规则 并设置3600秒的缓存
Route::get('new/:id','News/read',[
	'cache'	=>	[ 'new/:id/:page',3600]
]);

:id、:page表示使用当前请求的param参数进行动态标识替换,也就是根据id和page变量进行3600秒的请求缓存。

如果cache参数传入false,则表示关闭当前路由的请求缓存(即使开启全局请求缓存)。

// 定义GET请求路由规则 并设置3600秒的缓存
Route::get('new/:id','News/read',[
	'cache'	=>	[ 'new/:id/:page',3600,'news']
]);

动态设置

可以在app_begin行为里面动态设置请求缓存,例如:

Request::instance()->cache('blog/:id',3600);

表示对blog/:id定义的动态访问地址进行3600秒的请求缓存。

变量支持当前的请求变量(也就是param方法的所有变量)。

可以使用当前的URL地址作为缓存标识,如下:

Request::instance()->cache('__URL__',600);

支持对某个URL后缀的请求进行缓存,例如:

equest::instance()->cache('[html]',600);

表示对所有的html后缀访问(GET)请求进行10分钟的缓存。

缓存标签设置

Request::instance()->cache('blog/:id',600,'blog');

请求缓存自动判断,只需要在配置文件中开启:

'request_cache'	=>	true,
'request_cache_expire'	=>	3600,

就会自动根据当前请求URL地址(只针对GET请求类型)进行请求缓存,全局缓存有效期为3600秒。

全局请求缓存支持设置排除规则,使用方法如下:

'request_cache'	=>	true,
'request_cache_expire'	=>	3600,
'request_cache_except' =>	[
	'/blog/index',
    '/user/member'
],

request_cache_except设置的规则为不使用请求缓存的地址(不支持变量)开头部分。

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值