預處理
新建自定義異常類
<?php
namespace App\Exceptions;
use Throwable;
use Exception;
class SwooleExitException extends Exception
{
protected $response;
public function __construct($response,$message = "", $code = 0, Throwable $previous = null)
{
$this->response = $response;
parent::__construct($message, $code, $previous);
}
//獲取響應內容
public function getResponse(){
return $this->response;
}
}
在Handler.php中,render方法中新增程式碼
protected $dontReport = [
//
SwooleExitException::class
];
public function render($request, Exception $exception)
{
if ($exception instanceof SwooleExitException) {
//直接呼叫perpare
return $exception->getResponse()->prepare($request);
}
}
swoole_exit 為自定義的全域性函式
if (!function_exists('swoole_exit')){
function swoole_exit($response)
{
throw new App\Exceptions\SwooleExitException($response);
}
}
接下來是改框架。小弟不才。沒有想到其他辦法。希望各位大神指點迷津。
小弟qq247552748
第一步
修改Encore\Admin\Grid\Exporters\CsvExporter.php 檔案
public function export()
{
.....
....
response()->stream($response, 200, $this->getHeaders())->send();
exit;
}
處理掉exit
改為
public function export()
{
.....
....
swoole_exit(response()->stream($response, 200, $this->getHeaders()));
}
第二步
Encore\Admin\Middleware\Pjax.php
跟第一步同樣道理,處理掉exit方式,修改為請求返回的形式
public static function respond(Response $response)
{
$next = function () use ($response) {
return $response;
};
swoole_exit((new static())->handle(Request::capture(), $next));
// (new static())->handle(Request::capture(), $next)->send();
// exit;
}
第三步
Encore\Admin\Grid\Exporters\ExcelExporter
public function export()
{
// $this->download($this->fileName)->prepare(request())->send();
swoole_exit($this->download($this->fileName)->prepare(request()));
// exit;
}
參考自 部落格:Laravel-admin 在 Laravels 執行的的一些問題解析——記一次因為莽而付...
本作品採用《CC 協議》,轉載必須註明作者和本文連結