使用writeJson不返回数据是为什么呢
来源:2-4 EasySwoole结合Mysql使用
慕少5137873
2019-11-15
这个是Api下的index
<?php
namespace App\HttpController\Api;
use App\HttpController\Api\Base;
use EasySwoole\Component\Di;
class Index extends Base
{
public function video()
{
//$config = new \EasySwoole\Mysqli\Config(config::getInstance()->getConf('MYSQL'));
$db = Di::getInstance()->get('MYSQL');
$db->queryBuilder()->getOne('video');
$result = $db->execBuilder();
$this->writeJson('201', 'ok', $result);
}
public function nihao()
{
//$this->response()->write('hhhhhh');
return $this->writeJson('101', 'ok');
}
}
这个是Base
<?php
namespace App\HttpController\Api;
use EasySwoole\Http\AbstractInterface\Controller;
/**
* Api下基础类库
* Class Base
* @package App\HttpController\Api
*/
class Base extends Controller
{
public function index()
{
// TODO: Implement index() method.
}
/**
* 程序有异常时报错
* @param \Throwable $throwable
*/
public function onException(\Throwable $throwable): void
{
// TODO: Change the autogenerated stub
//记录日志,待完善
$this->writeJson(400, '','内部有异常');
}
/**
* Api返回Json数据格式
* @param int $statusCode
* @param null $message
* @param null $result
* @return bool
*/
protected function writeJson($statusCode = 200, $message = null, $result = null){
if(!$this->response()->isEndResponse()){
$data = Array(
"code"=>$statusCode,
"message"=>$message,
"result"=>$result
);
$this->response()->write(json_encode($data,JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES));
$this->response()->withHeader('Content-type','application/json;charset=utf-8');
$this->response()->withStatus($statusCode);
return true;
}else{
return false;
}
}
}
写回答
1回答
-
图1中可以,图2不可以吗?
032019-11-18
相似问题