0

我正在创建一个 API 服务器 STUB,我希望它尽可能简单,所以我决定不使用 Laravel 框架。

我的大部分应用程序都在运行,但我正处于客户端 SDK 将 POST 参数发送到我的服务器 STUB 并且想要使用 Illuminate Request 库而不是 $_POST

这是我现在的应用流程(实际问题在底部解释)

我的公共目录中的 index.php 正在调用我的 bootstrap.php 文件,该文件负责加载供应商自动加载和数据库连接

<?php
require 'vendor/autoload.php';

use Dotenv\Dotenv;
use Src\System\DatabaseConnector;

$dotenv = new DotEnv(__DIR__);
$dotenv->load();

$DatabaseConnector = new DatabaseConnector();
$dbConnection = $DatabaseConnector->getConnection();
$tkObj = $DatabaseConnector->getToolKit();

然后稍后在我的 index.php 中我调用我的控制器

$controller = new OrderValidationController($dbConnection, $tkObj, $requestMethod);
$controller->processRequest();

我的控制器如下所示:

<?php
namespace Src\Controller;
use Src\Gateway\OrderValidationGateway;
use Src\Gateway\ApiLogGateway;

class OrderValidationController {

    private $db;
    private $tkObj;
    private $requestMethod;
    private $orderValidation;

    public function __construct($db, $tkObj, $requestMethod)
    {
        $this->db = $db;
        $this->tkObj = $tkObj;
        $this->requestMethod = $requestMethod;
        $this->OrderValidation = new OrderValidationGateway($db, $tkObj);
    }

    public function processRequest()
    {

        switch ($this->requestMethod) {
            case 'GET':
                break;
            case 'POST':
                $response = $this->postOrderValidation();
                break;
            case 'PUT':
                break;
            case 'DELETE':
                break;
            default:
                $response = $this->notFoundResponse();
                break;
        }
        header($response['status_code_header']);
        if ($response['body']) {
            echo $response['body'];
        }
    }

    private function unprocessableEntityResponse()
    {
        $response['status_code_header'] = 'HTTP/1.1 422 Unprocessable Entity';
        $response['body'] = json_encode([
            'error' => 'Invalid input'
        ]);
        return $response;

        $ApiLogGateway = new ApiLogGateway($this->db);
        $ApiLogGateway->jsonEventLog($response['body']);
    }

    private function notFoundResponse()
    {
        // Debug Info
        $response['status_code_header'] = 'HTTP/1.1 404 Not Found';
        $response['body'] = null;
        return $response;
    }

    private function postOrderValidation()
    {
        $result = $this->OrderValidation->OrderValidation();
        $response['status_code_header'] = 'HTTP/1.1 200 OK';
        $response['body'] = json_encode($result);
        return $response;
    }

}
?>

控制器调用网关

<?php
namespace Src\Gateway;
use Illuminate\Http\Request;

class OrderValidationGateway {

    private $db = null;
    private $tkObj = null;

    public function __construct($db, $tkObj)
    {
        $this->db = $db;
        $this->tkObj = $tkObj;
    }

    public function OrderValidation(){

        $this->tkObj->CLCommand("ADDLIBLE LIB(G2001RY)");

        die($this->req->PV_SITE);

        $ins = array(
            "PV_SITE"=>array("type"=>"CHAR","value"=>$request->PV_SITE, "length"=>"2", "operation"=>'in'),
            "PV_DEP"=>array("type"=>"CHAR","value"=>$request->PV_DEP, "length"=>"8", "operation"=>'in'),
            "PV_WAVEID"=>array("type"=>"DECIMAL","value"=>$request->PV_WAVEID, "length"=>"7,0", "operation"=>'in'),
            "PV_DOCNBR"=>array("type"=>"DECIMAL","value"=>$request->PV_DOCNBR, "length"=>"7,0", "operation"=>'in'),
            "PV_DOID"=>array("type"=>"DECIMAL","value"=>$request->PV_DOID, "length"=>"7,0", "operation"=>'in'),
            "PV_DOCLIN"=>array("type"=>"DECIMAL","value"=>$request->PV_DOCLIN, "length"=>"4,0", "operation"=>'in'),
            "PV_MVTNBR"=>array("type"=>"DECIMAL","value"=>$request->PV_MVTNBR, "length"=>"9,0", "operation"=>'in'),
            "PV_WRH"=>array("type"=>"CHAR","value"=>$request->PV_WRH, "length"=>"3", "operation"=>'in'),
            "PV_ADDR"=>array("type"=>"CHAR","value"=>$request->PV_ADDR, "length"=>"8", "operation"=>'in'),
            "PV_EXTCOD"=>array("type"=>"CHAR","value"=>$request->PV_EXTCOD, "length"=>"8", "operation"=>'in'),
            "PV_OBJNBR"=>array("type"=>"DECIMAL","value"=>$request->PV_OBJNBR, "length"=>"9,0", "operation"=>'in'),
            "PV_ITM"=>array("type"=>"CHAR","value"=>$request->PV_ITM, "length"=>"20", "operation"=>'in'),
            "PV_STKNAT"=>array("type"=>"CHAR","value"=>$request->PV_STKNAT, "length"=>"2", "operation"=>'in'),
            "PV_LOT"=>array("type"=>"CHAR","value"=>$request->PV_LOT, "length"=>"13", "operation"=>'in'),
            "PV_CNTNBR"=>array("type"=>"DECIMAL","value"=>$request->PV_CNTNBR, "length"=>"7,0", "operation"=>'in'),
            "PV_CNTTYP"=>array("type"=>"CHAR","value"=>$request->PV_CNTTYP, "length"=>"1", "operation"=>'in'),
            "PV_CNTCOD"=>array("type"=>"CHAR","value"=>$request->PV_CNTCOD, "length"=>"2", "operation"=>'in'),
            "PV_QTYUNT"=>array("type"=>"DECIMAL","value"=>$request->PV_QTYUNT, "length"=>"7,0", "operation"=>'in'),
            "PV_LPNNBR"=>array("type"=>"CHAR","value"=>$request->PV_LPNNBR, "length"=>"25", "operation"=>'in'),
            "PV_MVTDAT"=>array("type"=>"CHAR","value"=>$request->PV_MVTDAT, "length"=>"10", "operation"=>'in'),
            "PV_MVTTIM"=>array("type"=>"CHAR","value"=>$request->PV_MVTTIM, "length"=>"8", "operation"=>'in'),
            "PV_SERIAL"=>array("type"=>"CHAR","value"=>$request->PV_SERIAL, "length"=>"20", "operation"=>'in'),
            "PV_USERID"=>array("type"=>"CHAR","value"=>$request->PV_USERID, "length"=>"20", "operation"=>'in')
        );

        $in = $this->arrayConvert($ins, $tkobj);

        $result = $tkobj->PgmCall("API_POVLD", "G2001RY", $in, NULL);

        return $result;

    }

    public function arrayConvert($arrays, $tkobj){

        foreach($arrays as $key=>$line){
            $in[] = $tkobj->AddParameterChar($line["operation"], $line["length"], $key, $key, $line["value"]);
        }

        return $in;

    }

    public function req(Request $request) {
        return $request;
    }

}

所以我的问题是我似乎无法使用 $this->req->PV_SITE 接收 POST 参数

在旁注中,我通过做它来工作

    $request = new Request();
    $request->replace($_POST);

但我认为 $request 是基于 GET、PUT、POST [...] 自动构建的。还是我错过了什么?

在此处输入图像描述

4

0 回答 0