华域联盟 PHP 关于中间件:slim3 php框架中response->getBody()为空

关于中间件:slim3 php框架中response->getBody()为空

我在使用这个 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 )
        {
                echo $response ;
                echo $response -> getBody ( ) -> getSize ( ) ;
                $response = $response -> getBody ( ) -> write ( json_encode ( 'holla' ) ) ;
                $response = $response -> withHeader ( 'Content-Type' , 'application/json; charset=utf-8' ) ;
                return $response ;
        }

        public function __invoke ( $request , $response , $next )
        {
                $userid = $request -> getHeaderLine ( 'userid' ) ;
                $authorization = $request -> getHeaderLine ( 'Authorization' ) ;
                if ( $this -> isAuthenticated ( $userid , $authorization ) )
                {
                        $response = $next ( $request , $response ) ;
                }
                else
                {
                        $msg = 'You are unauthenticated. Please login again' ;
                        $code = 400 ;
                        $response = $this -> createErrorResponse ( $code , $msg , $response ) ;
                }
                return $response ;
        }
}



相关讨论

  • 只是让其他人都知道。我似乎暴露了框架中的一个错误,他们正在讨论它。当你看到这个时,它会被修复。由于修复这个错误的时间框架,我将完全放弃 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\
equest ;
use Slim\\Http\
esponse ;

class AuthenticationMiddleware {

    public function isAuthenticated ( $userid , $authorization ) {
        //do data validation here
        return false ;
    }

    public function createErrorResponse ( $code , $msg , Response $response ) {
        return $response -> withStatus ( $code )
            -> withHeader ( 'Content-Type' , 'application/json;charset=utf-8' )
            -> withJson ( $msg ) ;
    }

    public function __invoke (Request $request , Response $response , $next ) {
        $userid = $request -> getHeaderLine ( 'userid' ) ;
        $authorization = $request -> getHeaderLine ( 'Authorization' ) ;
        if ( ! $this -> isAuthenticated ( $userid , $authorization ) ) {
            $msg = 'You are unauthenticated. Please login again' ;
            $code = 400 ;
            $this -> createErrorResponse ( $code , $msg , $response ) ;
        } else {
            $response = $next ( $request , $response ) ;
        }
        return $response ;
    }
}

我就这么说吧。我会在这段代码中抽象一些东西,这样我就不会重复自己了。我看到你在重复:

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

本文来自网络,不代表华域联盟立场,转载请注明出处。

作者: sterben

发表回复

联系我们

联系我们

2551209778

在线咨询: QQ交谈

邮箱: [email protected]

工作时间:周一至周五,9:00-17:30,节假日休息

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

关注微博
返回顶部