当前位置:网站首页>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
边栏推荐
- 与张小姐的春夏秋冬(3)
- 闪贷Dapp的调研及实现
- July 28 ens/usd Value Forecast: ENS attracts huge profits
- 极致通缩和永动机模型,将推动 PlatoFarm 爆发
- 与张小姐的春夏秋冬(5)
- SQL修复重复数据
- 一文读懂Move2Earn项目——MOVE
- 以‘智’提‘质|金融影像平台解决方案
- Simple optimization of interesting apps for deep learning (suitable for novices)
- The bear market is slow, and bit.store provides stable stacking products to help you get through the bull and bear market
猜你喜欢

Crypto giants all in metauniverse, and platofarm may break through

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

Gluster cluster management analysis

C# 连接 SharepointOnline WebService

File文件上传的使用(2)--上传到阿里云Oss文件服务器

农村品牌建设给年轻人的一些机会

第五空间智能安全⼤赛真题----------PNG图⽚转换器

新手入门:手把手从PHP环境到ThinkPHP6框架下载

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

Fvuln automated web vulnerability detection tool
随机推荐
与张小姐的春夏秋冬(1)
以‘智’提‘质|金融影像平台解决方案
微信内置浏览器禁止缓存的问题
Fantom (FTM) surged 45% before the FOMC meeting
Super simple integration of HMS ml kit to realize parent control
超简单集成HMS ML Kit 实现parental control
Countdown of the uniapp component (such as the countdown to reading the agreement and the countdown to completing learning)
DAY4:MySQL 数据库的建立及简单实用
From starfish OS' continued deflationary consumption of SFO, the value of SFO in the long run
Novice introduction: download from PHP environment to thinkphp6 framework by hand
Shanzhai coin Shib has a US $548.6 million stake in eth whale's portfolio - traders should be on guard
剑指核心-TaoCloud全闪SDS助力构建高性能云服务
Laravel service container (inheritance and events)
Read and understand move2earn project - move
如何零代码制作深度学习的趣味app(适合新手)
Laravel swagger add access password
XDFS&空天院HPC集群典型案例
闪贷Dapp的调研及实现
『全闪实测』数据库加速解决方案
Xsan is highly available - xdfs and San are integrated with new vitality