通过邮递员发送“服务器参数”:

时间:2020-09-24 10:12:53

标签: symfony postman symfony4 symfony-3.4

由于 Symfony HttpFoundation组件,我们可以像以下脚本一样检索服务器参数

   // retrieves SERVER variables
    $request->server->get('HTTP_HOST')

所以,我有以下构造,我想拥有服务器参数:

public function __construct(RequestStack $requestStack)
{
$request = $requestStack->getCurrentRequest();

$this->country = self::DEFAULT_COUNTRY;
$this->lang = self::DEFAULT_LANG;
$this->brand = self::DEFAULT_BRAND;
$this->jobBrand = $this->brand;

if ($request) {
    if (!empty($request->server->get(self::ENV_COUNTRY_CODE))) {
        $this->country = $request->server->get(self::ENV_COUNTRY_CODE);
    }

    if (!empty($request->server->get(self::ENV_LANG_CODE))) {
        $this->lang = $request->server->get(self::ENV_LANG_CODE);
    }

    if (!empty($request->server->get(self::ENV_BRAND))) {
        $this->jobBrand = $request->server->get(self::ENV_BRAND);
        $this->brand = str_replace('pro', '', $this->jobBrand);
    }

    if (empty($this->country) || empty($this->lang)) {
        throw new NoApacheLocaleException();
    }
}
}

有关信息,在测试阶段,我使用 Postman 作为http客户端。

所以我的问题是:如何通过邮递员发送参数,以便通过 $ request-> server-> get('param') 来获取它?

1 个答案:

答案 0 :(得分:0)

根据@Cerad,我对他的回答深信不疑:

$ _ SERVER 由PHP根据服务器端信息初始化。不使用HTTP请求中的任何内容。我想您可以使用变量名称(例如 _server_http_host )来伪造它以进行测试,并相应地调整代码。但是,如果您使用的是Symfony框架,那么使用Symfony的功能测试方法可能会更好。