报错内容:
PHP Fatal error: Uncaught TypeError: Argument 1 passed to Hyperf\HttpServer\ResponseEmitter::emit() must implement interface Psr\Http\Message\ResponseInterface, instance of Hyperf\ViewEngine\View given, called in /www/wwwroot/server/vendor/hyperf/http-server/src/Server.php on line 128 and defined in /www/wwwroot/server/vendor/hyperf/http-server/src/ResponseEmitter.php:24
报错代码:
use Psr\Http\Message\ResponseInterface; use function Hyperf\ViewEngine\view; public function handle(ResponseInterface $response) { return view('/Common/Response/error', [ 'code' => 500, 'msg' => "发生错误" 'data' => [], ]); }
分析:在hyperf开发过程中会遇到方法或类需要实现接口的问题,这说明报错的方法或类强制要求对象指针实现相关接口
解决方法有两种情况:
1.已经引入需实现的接口,此时只需用其返回即可
use Psr\Http\Message\ResponseInterface; use Hyperf\HttpMessage\Stream\SwooleStream; use function Hyperf\ViewEngine\view; public function handle(ResponseInterface $response) { return $response ->withHeader("Content-Type", "text/html;charset=utf-8") ->withStatus(200) ->withBody(new SwooleStream((string) view('/Common/Response/error', [ 'code' => 500, 'msg' => "发生错误" 'data' => [], ]))); }
2.未引入,利用协程切换到需实现的接口,进行相关业务调用即可
use Psr\Http\Message\ResponseInterface; use Hyperf\HttpMessage\Stream\SwooleStream; use Hyperf\Utils\Context; use function Hyperf\ViewEngine\view; public function handle() { return Context::get(ResponseInterface::class) ->withHeader("Content-Type", "text/html;charset=utf-8") ->withStatus(200) ->withBody(new SwooleStream((string) view('/Common/Response/error', [ 'code' => 500, 'msg' => "发生错误" 'data' => [], ]))); }
无论从事什么行业,只要做好两件事就够了,一个是你的专业、一个是你的人品,专业决定了你的存在,人品决定了你的人脉,剩下的就是坚持,用善良專業和真诚赢取更多的信任。