当前位置:网站首页>Thinkphp6 output QR code image format to solve the conflict with debug
Thinkphp6 output QR code image format to solve the conflict with debug
2022-07-29 05:51:00 【Liaoshengping】
demand , Dynamically output QR code pictures .
If it's on Debug Pattern , There is interference from debugging tools , The picture cannot be displayed normally , If there is no such problem in the formal environment . But I started in a formal environment debug Pattern , Debug error , Then the problem is GG 了 .
like this :

An extreme approach is direct echo picture exit , Just do it
header('Content-Type:'.$result->getMimeType());
echo $result->getString();
exit;
In this way, you can output pictures
If only to complete the requirements , Just as above .
As a good programmer, you should not compromise for the framework , But fix it .
Whether to use exit perhaps die End your program , It's not recommended :
The exit(...) and die(...) statements should absolutely not be used in Web PHP pages as this might lead to a very bad user experience. In such case, the end user might have the feeling that the web site is down or has encountered a fatal error.
But of course PHP can also be used to develop command line application and in such case use of exit(...) or die(...) statement can be justified but must remain limited and not spread all over the application. We expect exceptions to be used to handle errors and those exceptions should be caught just before leaving the application to specify the exit code with help of exit(...) or die(...) statements.
Reference resources : “exit(…)” and “die(…)” statements should not be used
Find the problem
Since it is debug The problem of , I put debug Just turn it off
$this->app->debug(false);
But the debugging toolbar still appears , ultimately , Find a way to deal with trace Class , To find out .
public function handle($request, Closure $next)
{
$debug = $this->app->isDebug();
// Register log listening
if ($debug) {
$this->log = [];
$this->app->event->listen(LogWrite::class, function ($event) {
if (empty($this->config['channel']) || $this->config['channel'] == $event->channel) {
$this->log = array_merge_recursive($this->log, $event->log);
}
});
}
$response = $next($request);
// Trace Debug injection
if ($debug) {
$data = $response->getContent();
$this->traceDebug($response, $data);
$response->content($data);
}
return $response;
}
Trace Service registration :
public function register()
{
$this->app->middleware->add(TraceDebug::class);
}
$response = $next($request); Middleware , The above is the record of the log , The following is the processing after the request is completed . You can see what you get here isDebug It is copied when the framework is loaded , That is to say, we modify it in the controller app In container debug It's invalid .
Change your mind
In the bottom judgment , If the return object is json, perhaps ajax The direct display of debug, therefore $contentType There should be no problem changing to image format .
public function output(App $app, Response $response, array $log = [])
{
$request = $app->request;
$contentType = $response->getHeader('Content-Type');
if ($request->isJson() || $request->isAjax()) {
return false;
} elseif (!empty($contentType) && strpos($contentType, 'html') === false) {
return false;
} elseif ($response->getCode() == 204) {
return false;
}
.
.
.
Look directly at the control output Respose Class processing , here Respose The creation of requires input and output content types , Find according to the namespace think Framework file
public static function create($data = '', string $type = 'html', int $code = 200): Response
{
$class = false !== strpos($type, '\\') ? $type : '\\think\\response\\' . ucfirst(strtolower($type));
return Container::getInstance()->invokeClass($class, [$data, $code]);
}

See light suddenly , The original output needs a file format , There happens to be no picture format here , So you need to create a class of image format by yourself :
<?php
namespace app\common\service\system\think;
use think\Cookie;
use think\Response;
class Image extends Response
{
/** * Output type * @var string */
protected $contentType = 'image/png';
public function __construct( $data = '', int $code = 200)
{
$this->init($data, $code);
}
}
Introduce the custom class , It can be avoided exit Bring risks
$content = $result->getString();
$res = Response::create($content,Image::class);
return $res;
It's written very loosely , Make a note of , Ideas for finding problems , I hope it helps you
边栏推荐
- 数组的基础使用--遍历循环数组求出数组最大值,最小值以及最大值下标,最小值下标
- Study and research the way of programming
- 打印出1-100之间的所有质数
- "Shandong University mobile Internet development technology teaching website construction" project training log I
- “山东大学移动互联网开发技术教学网站建设”项目实训日志四
- How to survive in the bear market of encryption market?
- “山东大学移动互联网开发技术教学网站建设”项目实训日志六
- Training log II of the project "construction of Shandong University mobile Internet development technology teaching website"
- “山东大学移动互联网开发技术教学网站建设”项目实训日志五
- “山东大学移动互联网开发技术教学网站建设”项目实训日志二
猜你喜欢

Super simple integration of HMS ml kit to realize parent control

IDEA使用JDBC连接MySQL数据库个人详细教程

“山东大学移动互联网开发技术教学网站建设”项目实训日志三

“山东大学移动互联网开发技术教学网站建设”项目实训日志五

iSCSI vs iSER vs NVMe-TCP vs NVMe-RDMA

Dao race track is booming. What are the advantages of m-dao?

大部分PHP程序员,都搞不懂如何安全代码部署

全闪分布式,如何深度性能POC?

熊市慢慢,Bit.Store提供稳定Staking产品助你穿越牛熊

深度学习的趣味app简单优化(适合新手)
随机推荐
如何在加密市场熊市中生存?
Flink connector Oracle CDC 实时同步数据到MySQL(Oracle12c)
Dao race track is booming. What are the advantages of m-dao?
Performance comparison | FASS iSCSI vs nvme/tcp
MOVE PROTOCOL全球健康宣言,将健康运动进行到底
C# 连接 SharepointOnline WebService
与张小姐的春夏秋冬(2)
How to make interesting apps for deep learning with zero code (suitable for novices)
Some opportunities for young people in rural brand building
DAY5:PHP 简单语法与使用
Super simple integration of HMS ml kit to realize parent control
完全去中心化的编程模式,不需要服务器,也不需要ip,就像一张漫无目的的网络、四处延伸
XDFS&空天院HPC集群典型案例
极致通缩和永动机模型,将推动 PlatoFarm 爆发
Win10+opencv3.2+vs2015 configuration
“山东大学移动互联网开发技术教学网站建设”项目实训日志七
"Shandong University mobile Internet development technology teaching website construction" project training log V
微信内置浏览器禁止缓存的问题
win10+opencv3.2+vs2015配置
量化开发必掌握的30个知识点【什么是Level-2数据】