通常我们在中间件里这样配置
app\Http\Middleware\EncryptCookies.php
/**
* The names of the cookies that should not be encrypted.
*
* @var array
*/
protected $except = [
'cookie_name',
];
如果我们的 cookie 名称是可变动态的该怎么办呢?
可以使用通配符这样处理:
/**
* The names of the cookies that should not be encrypted.
*
* @var array
*/
protected $except = [
'cookie_name',
'cookie_name_*',
];
public function isDisabled($name)
{
if (in_array($name, $this->except))
{
return true;
}
foreach ($this->except as $pattern)
{
if (Str::is($pattern, $name))
{
return true;
}
}
return false;
}
或者使用正则:
/**
* The names of the cookies that should not be encrypted.
*
* @var array
*/
protected $except = [
'cookie_name',
'/^cookie_name_\d+$/',
];
public function isDisabled($name)
{
if (in_array($name, $this->except))
{
return true;
}
foreach ($this->except as $pattern)
{
if (preg_match($pattern, $name))
{
return true;
}
}
return false;
}
这个功能在strongshop 开源商城也有所应用
Gitee 地址:https://gitee.com/openstrong/strongshop