The process for replacing the deprecated HttpUtility::redirect() with PSR-7 response in a legacy TYPO3 plugin is as follows:
Ensure that the plugin is using TYPO3 version 9 or higher, which supports the PSR-7 standard.
Create a new class for the PSR-7 response using the following code:
use Psr\Http\Message\ResponseInterface; use TYPO3\CMS\Core\Http\Response; use TYPO3\CMS\Core\Utility\HttpUtility;
class Psr7Response implements ResponseInterface { protected $headers = []; protected $statusCode = 200; protected $reasonPhrase = '';
public function __construct($statusCode = 200, array $headers = [])
{
$this->setStatusCode($statusCode);
$this->headers = $headers;
}
public function getStatusCode()
{
return $this->statusCode;
}
public function setStatusCode($code, $reasonPhrase = null)
{
$this->statusCode = (int) $code;
if ($reasonPhrase === null) {
$reasonPhrase = isset(Response::$statusTexts[$this->statusCode]) ? Response::$statusTexts[$this->statusCode] : '';
}
$this->reasonPhrase = $reasonPhrase;
return $this;
}
public function getReasonPhrase()
{
return $this->reasonPhrase;
}
public function getHeaders()
{
return $this->headers;
}
public function getHeader($name)
{
return isset($this->headers[$name]) ? $this->headers[$name] : null;
}
public function withHeader($name, $value)
{
$clone = clone $this;
$clone->headers[$name] = [$value];
return $clone;
}
public function withAddedHeader($name, $value)
{
$clone = clone $this;
if (isset($clone->headers[$name])) {
$clone->headers[$name][] = $value;
} else {
$clone->headers[$name] = [$value];
}
return $clone;
}
public function withoutHeader($name)
{
$clone = clone $this;
unset($clone->headers[$name]);
return $clone;
}
public function hasHeader($name)
{
return isset($this->headers[$name]);
}
public function getBody()
{
return null;
}
public function withBody(StreamInterface $body)
{
$clone = clone $this;
return $clone;
}
public function __toString()
{
HttpUtility::redirect();
}
}
use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface;
class ExampleController { public function handleRequest(ServerRequestInterface $request): ResponseInterface { $url = 'http://www.example.com';
$response = new Psr7Response(301, ['Location' => $url]);
return $response;
}
}
Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss
Asked: 2022-07-03 11:00:00 +0000
Seen: 10 times
Last updated: Jun 01 '22
What is the process of using the Multmerge() function in r to combine files in a directory?
Is it possible for two distinct useState functions to trigger changes simultaneously?
How can one use node to change an ogg file into an mp3 file?
How can a function pointer be transferred between different files in CUDA?
What does the error message "TypeError: e.endsWith is not a function" mean in Redoc?
What is the expected outcome of the math.floor function in terms of the return value?