我在使用这个 slim3 php 代码时遇到了问题。在函数 createErrorReponse 函数中,$response->getBody() 为 null 或空。 PHP 在下面抱怨以下错误。如您所见, getBody() 大小为空,因此 write 无法处理它。不过,同一行也适用于其他功能。
HTTP/1.1 200 OK 内容类型:text/html;字符集=UTF-8 0
致命错误:在第 16 行的 /home/ubuntu/webapp/middleware/authmodule.php 中的非对象上调用成员函数 withHeader()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
<?php class AuthenticationMiddleware { public function isAuthenticated ( $userid , $authorization ) { //do data validation here return false ; } public function createErrorResponse ( $code , $msg , $response ) public function __invoke ( $request , $response , $next ) |
- 只是让其他人都知道。我似乎暴露了框架中的一个错误,他们正在讨论它。当你看到这个时,它会被修复。由于修复这个错误的时间框架,我将完全放弃 php 和这个糟糕的框架。
感谢您提供有关错误报告和修复的提示。
我觉得有必要回答这个问题,以防万一你还没有完全放弃 PHP 和 Slim 框架。
希望对其他人有所帮助。
我的做法是:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
<?php
use Slim\\Http\ class AuthenticationMiddleware { public function isAuthenticated ( $userid , $authorization ) { public function createErrorResponse ( $code , $msg , Response $response ) { public function __invoke (Request $request , Response $response , $next ) { |
我就这么说吧。我会在这段代码中抽象一些东西,这样我就不会重复自己了。我看到你在重复:
1 2 3 4 5 |
public function createErrorResponse ( $code , $msg , Response $response ) { return $response -> withStatus ( $code ) -> withHeader ( 'Content-Type' , 'application/json;charset=utf-8' ) -> withJson ( $msg ) ; } |
在你所有的中间件中,也许在你的路由中。
希望这能让某人走上正轨。
本文由 华域联盟 原创撰写:华域联盟 » 关于中间件:slim3 php框架中response->getBody()为空
转载请保留出处和原文链接:https://www.cnhackhy.com/158103.htm