-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
404s cannot be sent in production without templating installed #25844
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
This seems related to or a duplicate of the closed issue #23203. |
It seems I am too in love with Flex and extremely lean apps. This is to be expected with no templating. The cure:
Do have a nice day everyone! |
Ah, but it should be possible to return a proper 404 without needing templating, correct? |
A quick workaround to avoid installing template is to add a "catch all" route as the last defined route, such as: routes.yaml:
DefaultController.php:
|
Here is the way to do the same with an event listener: services.yaml:
EventListener.php:
|
We are experiencing the same behaviour Our api project has no need for templates. All requests that don't match a route generate 200 responses in prod environment. We would expect 404 instead Happy to work on a patch if this is acknowledged as an issue and some guidance is provided |
@rodrigodiez 200s or 500s? This is what I have been using as a workaround: namespace App\EventSubscriber;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\HttpKernel\KernelInterface;
class ExceptionSubscriber implements EventSubscriberInterface
{
private $kernel;
public function __construct(KernelInterface $kernel)
{
$this->kernel = $kernel;
}
public static function getSubscribedEvents()
{
return [
KernelEvents::EXCEPTION => [
['processException', 0],
]
];
}
public function processException(GetResponseForExceptionEvent $event)
{
/**
* This is needed until Symfony can handle converting
* HttpException without template installed.
*
* @see https://github.com/symfony/symfony/issues/25844
*/
if (!$this->kernel->isDebug()) {
$exception = $event->getException();
$response = new Response();
if ($exception instanceof HttpExceptionInterface) {
$code = $exception->getStatusCode();
$response->setStatusCode($code);
$message = isset(Response::$statusTexts[$code]) ? Response::$statusTexts[$code] : '';
$response->setContent($message);
} else {
$response->setStatusCode(Response::HTTP_INTERNAL_SERVER_ERROR);
}
$event->setResponse($response);
}
}
} |
@cilefen I am getting 200s composer create-project symfony/skeleton symfony-404-bug-test
echo "APP_ENV=prod" > symfony-404-bug-test/.env
docker run --rm -p "80:80" -v "${PWD}/symfony-404-bug-test:/var/www/html" php:7.2-apache
curl -i http://localhost/public/index.php/foo Results in
|
…nstalled (cilefen) This PR was merged into the 3.4 branch. Discussion ---------- [HttpKernel] Catch HttpExceptions when templating is not installed | Q | A | ------------- | --- | Branch? | 3.4 | Bug fix? | yes | New feature? | no | BC breaks? | ? | Deprecations? | no | Tests pass? | ? | Fixed tickets | #25844 | License | MIT | Doc PR | symfony/symfony-docs#... <!--highly recommended for new features--> - [x] Test manually - [x] Check for BC breaks - [x] Needs tests <!-- - Bug fixes must be submitted against the lowest branch where they apply (lowest branches are regularly merged to upper ones so they get the fixes too). - Features and deprecations must be submitted against the master branch. - Replace this comment by a description of what your PR is solving. --> Commits ------- 4e527aa bug #25844 [HttpKernel] Catch HttpExceptions when templating is not installed
* 3.4: PhpDoc: There is no attempt to create the directory Avoiding an error when an unused service has a missing base class Add an implementation just for php 7.0 bumped Symfony version to 2.7.47 Fix #27011: Session ini_set bug [Cache] TagAwareAdapterInterface::invalidateTags() should commit deferred items updated VERSION for 2.7.46 update CONTRIBUTORS for 2.7.46 updated CHANGELOG for 2.7.46 bug #25844 [HttpKernel] Catch HttpExceptions when templating is not installed
* 4.0: PhpDoc: There is no attempt to create the directory Avoiding an error when an unused service has a missing base class Add an implementation just for php 7.0 bumped Symfony version to 2.7.47 Fix #27011: Session ini_set bug [Cache] TagAwareAdapterInterface::invalidateTags() should commit deferred items updated VERSION for 2.7.46 update CONTRIBUTORS for 2.7.46 updated CHANGELOG for 2.7.46 bug #25844 [HttpKernel] Catch HttpExceptions when templating is not installed
This is going to be reverted as "not a bug", and considered as a new feature instead. |
…HTTP status codes by default (nicolas-grekas) This PR was merged into the 4.2-dev branch. Discussion ---------- [HttpKernel][FrameworkBundle] Turn HTTP exceptions to HTTP status codes by default | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #25844 | License | MIT | Doc PR | - When an exception is thrown, *if it is not handled* then it will be reinjected as a 2nd exception event via `HttpKernel::terminateWithException()`. When this happens, this will generate a proper HTTP status code. Commits ------- 80b0739 [HttpKernel][FrameworkBundle] Turn HTTP exceptions to HTTP status codes by default
Another quick workaround for those making APIs until #27519 is released... index.php: $kernel = new Kernel($env, $debug);
$request = Request::createFromGlobals();
try {
$response = $kernel->handle($request);
$response->send();
} catch (\Symfony\Component\HttpKernel\Exception\NotFoundHttpException $e) {
$response = new \Symfony\Component\HttpFoundation\Response("404 Not Found", 404);
$response->send();
}
$kernel->terminate($request, $response); (edited) |
Steps to reproduce:
Create a project:
Set the app environment to prod:
This can be shown also with setting the APP_ENV environment variable.
Add a simple controller and a route.
Add an
.htaccess
file in /public as follows (we use Apache):Try to get "/foo". In the prod environment, ResourceNotFoundException is not caught or converted to a 404:
The text was updated successfully, but these errors were encountered: