zendframework / zend-expressive

PSR-15 middleware in minutes!
BSD 3-Clause "New" or "Revised" License
710 stars 197 forks source link

NotFoundHandler generateTemplatedResponse Content Type missing #647

Open cgaube opened 5 years ago

cgaube commented 5 years ago

I believe "Content-Type" header is missing from the newly created response object when a 404 error is handled.

/src/Handler/NotFoundHandler.php

/**
     * Generates a response using a template.
     *
     * Template will receive the current request via the "request" variable.
     */
    private function generateTemplatedResponse(
        TemplateRendererInterface $renderer,
        ServerRequestInterface $request
    ) : ResponseInterface {

        $response = ($this->responseFactory)()->withStatus(StatusCodeInterface::STATUS_NOT_FOUND);
        $response->getBody()->write(
            $renderer->render($this->template, ['request' => $request, 'layout' => $this->layout])
        );

        return $response;
    }

Should be

/**
     * Generates a response using a template.
     *
     * Template will receive the current request via the "request" variable.
     */
    private function generateTemplatedResponse(
        TemplateRendererInterface $renderer,
        ServerRequestInterface $request
    ) : ResponseInterface {

        $response = ($this->responseFactory)()
                 ->withHeader('Content-Type', 'text/html')
                 ->withStatus(StatusCodeInterface::STATUS_NOT_FOUND);
        $response->getBody()->write(
            $renderer->render($this->template, ['request' => $request, 'layout' => $this->layout])
        );

        return $response;
    }
plasid commented 5 years ago

Not 100% sure if this relates to your problem, but I get a server 500 error on anything "that goes wrong" in my application, even for 404's. - when I switch PHP error reporting off then all works... which is clearly a sign of bad architecture. One would imagine that a framework with PSR-7 & 15 at its roots would have the basics of HTTP sorted, clearly not... I'm so tired of wasting time with Zend's convoluted configuration and over-engineered concepts, and code examples which are naive or simply does not work.... wish I could say, I'm done with Zend and going back to PHP Slim, but the previous developers made the mistake of choosing Zend Expressive and now we are stuck with this bad experience.... and reminds me of why ZF never became popular and why I left ZF1-2 years ago.

weierophinney commented 4 years ago

This repository has been closed and moved to mezzio/mezzio; a new issue has been opened at https://github.com/mezzio/mezzio/issues/5.