-1

この Slim3 php コードに問題があります。関数 createErrorReponse 関数で、$response->getBody() が null または空です。PHPは、以下のエラーを訴えます。ご覧のとおり、 getBody() サイズは空であるため、書き込みは機能しません。ただし、同じ行は他の機能でも機能します。HTTP/1.1 200 OK コンテンツ タイプ: テキスト/html。charset=UTF-8 0 致命的なエラー: 16 行目の /home/ubuntu/webapp/middleware/authmodule.php の非オブジェクトに対するメンバー関数 withHeader() の呼び出し

<?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;
        }
}
4

1 に答える 1

4

バグレポートと修正についてご連絡いただきありがとうございます。ただし、PHP と Slim フレームワークを完全に捨てていない場合に備えて、この質問に回答する必要があると感じています。うまくいけば、それは他の誰かに役立つでしょう。

これに対する私のアプローチは次のようになります。

<?php

use Slim\Http\Request;
use Slim\Http\Response;

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;
    }
}

これだけ言わせてください。このコードでは、ここでいくつかのことを抽象化して、同じことを繰り返さないようにします。私はあなたが繰り返しているのを見ます:

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

すべてのミドルウェアで、おそらくルートで。これが誰かを正しい軌道に乗せることを願っています。

于 2016-03-08T13:19:43.257 に答える