简体   繁体   English

麻烦添加HTTP标头php

[英]Troubles with add http headers php

Ok, I creating blog, just for learn php. 好的,我创建博客只是为了学习php。 And when I try redirect page after Sign in or sign up, I use header("Location: /") for example, for redirect to home page of blog. 当我在登录或注册后尝试重定向页面时,例如,使用header(“ Location:/”)重定向到博客的首页。 And it's work only when I use var_dump()... This my code, help pls 而且只有当我使用var_dump()时它才起作用...这是我的代码,请

This method from Mediator, which send response for user and here headers added. 来自Mediator的此方法,它为用户发送响应,并在此添加了标头。

 /**
 * @param ResponseInterface $response
 */
public function sendResponse(ResponseInterface $response)
{
    $response->addHeaders();
    var_dump($response->getHeaders());
    http_response_code($response->getStatusCode());
    if ($response->getStatusCode() !== 200) {

        die($response->getContent());
    }
    echo $response->getContent();
}

This method from controller with sign up 从控制器进行注册的方法

/**
 * @return ResponseInterface
 * @throws \ReflectionException
 * @throws \core\FileNotFoundException
 */
public function signInAction()
{
    $authForm = $this->createAuthForm();

    if ($authForm->load()) {
        if ($authForm->isValid() && $authForm->login()) {
            return new SuccessResponse('Welcome!',[ '0' => 'Location: /',]);
        }
        ErrorsCheckHelper::setError('Wrong login or password');
        return $this->renderLogin();
    }

    return $this->renderLogin();
}

And this is my class for response which I used in this case 这是我在这种情况下使用的响应类

namespace response;
class SuccessResponse implements ResponseInterface{
/** @var int */
protected $statusCode = 200;
/** @var string */
protected $content;

/** @var array */
protected $headers;

/**
 * SuccessResponse constructor.
 * @param $headers
 * @param $content
 */
public function __construct(string $content, array $headers = array())
{
    $this->headers = $headers;
    $this->content = $content;
}

/**
 * @return array
 */
public function getHeaders(): array
{
    return $this->headers;
}

/**
 * @return string
 */
public function getContent(): string
{
    return $this->content;
}

/**
 * @param string $content
 */
public function setContent(string $content): void
{
    $this->content = $content;
}

/**
 * @return int
 */
public function getStatusCode(): int
{
    return $this->statusCode;
}


/**
 * @param array $headers
 */
public function setHeaders(array $headers)
{
    array_merge($this->headers, [$headers]);
}

public function addHeaders()
{
    if (!empty($this->headers)) {
        foreach ($this->headers as $header) {
            header($header);
        }
    }
    return;
}
}

The values of the headers that you want to set should be indexed by their name, not by a numeric value or other string, so where you had this: 您要设置的标头的值应通过其名称而不是数字值或其他字符串来索引,因此在此位置:

      return new SuccessResponse('Welcome!',[ '0' => 'Location: /',]);

try using: 尝试使用:

      return new SuccessResponse('Welcome!',[ 'Location: /']);

Oh, just whaaaaat..... I fix code like this and it's start work 哦,我只是.....我修复了这样的代码,它开始工作了

/**
 * @param ResponseInterface $response
 */
public function sendResponse(ResponseInterface $response)
{
    http_response_code($response->getStatusCode());
    if ($response->getStatusCode() !== 200) {
        $response->addHeaders();
        die($response->getContent());
    }
    $response->addHeaders();
    echo $response->getContent();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM