vendor/contao/core-bundle/src/Resources/contao/pages/PageError404.php line 55

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of Contao.
  4.  *
  5.  * (c) Leo Feyer
  6.  *
  7.  * @license LGPL-3.0-or-later
  8.  */
  9. namespace Contao;
  10. use Contao\CoreBundle\Exception\ForwardPageNotFoundException;
  11. use Contao\CoreBundle\Exception\PageNotFoundException;
  12. use Contao\CoreBundle\Util\LocaleUtil;
  13. use Symfony\Component\HttpFoundation\Response;
  14. /**
  15.  * Provide methods to handle an error 404 page.
  16.  *
  17.  * @author Leo Feyer <https://github.com/leofeyer>
  18.  */
  19. class PageError404 extends Frontend
  20. {
  21.     /**
  22.      * Generate an error 404 page
  23.      */
  24.     public function generate()
  25.     {
  26.         /** @var PageModel $objPage */
  27.         global $objPage;
  28.         $obj404 $this->prepare();
  29.         $objPage $obj404->loadDetails();
  30.         // Reset inherited cache timeouts (see #231)
  31.         if (!$objPage->includeCache)
  32.         {
  33.             $objPage->cache 0;
  34.             $objPage->clientCache 0;
  35.         }
  36.         /** @var PageRegular $objHandler */
  37.         $objHandler = new $GLOBALS['TL_PTY']['regular']();
  38.         header('HTTP/1.1 404 Not Found');
  39.         $objHandler->generate($objPage);
  40.     }
  41.     /**
  42.      * Return a response object
  43.      *
  44.      * @return Response
  45.      */
  46.     public function getResponse()
  47.     {
  48.         /** @var PageModel $objPage */
  49.         global $objPage;
  50.         $obj404 $this->prepare();
  51.         $objPage $obj404->loadDetails();
  52.         // Reset inherited cache timeouts (see #231)
  53.         if (!$objPage->includeCache)
  54.         {
  55.             $objPage->cache 0;
  56.             $objPage->clientCache 0;
  57.         }
  58.         /** @var PageRegular $objHandler */
  59.         $objHandler = new $GLOBALS['TL_PTY']['regular']();
  60.         return $objHandler->getResponse($objPage)->setStatusCode(404);
  61.     }
  62.     /**
  63.      * Prepare the output
  64.      *
  65.      * @return PageModel
  66.      *
  67.      * @internal Do not call this method in your code. It will be made private in Contao 5.0.
  68.      */
  69.     protected function prepare()
  70.     {
  71.         // Find the matching root page
  72.         $objRootPage $this->getRootPageFromUrl();
  73.         // Forward if the language should be but is not set (see #4028)
  74.         if ($objRootPage->urlPrefix && System::getContainer()->getParameter('contao.legacy_routing'))
  75.         {
  76.             // Get the request string without the script name
  77.             $strRequest Environment::get('relativeRequest');
  78.             // Only redirect if there is no language fragment (see #4669)
  79.             if ($strRequest && !preg_match('@^[a-z]{2}(-[A-Z]{2})?/@'$strRequest))
  80.             {
  81.                 // Handle language fragments without trailing slash (see #7666)
  82.                 if (preg_match('@^[a-z]{2}(-[A-Z]{2})?$@'$strRequest))
  83.                 {
  84.                     $this->redirect(Environment::get('request') . '/'301);
  85.                 }
  86.                 else
  87.                 {
  88.                     if ($strRequest == Environment::get('request'))
  89.                     {
  90.                         $strRequest LocaleUtil::formatAsLanguageTag($objRootPage->language) . '/' $strRequest;
  91.                     }
  92.                     else
  93.                     {
  94.                         $strRequest Environment::get('script') . '/' LocaleUtil::formatAsLanguageTag($objRootPage->language) . '/' $strRequest;
  95.                     }
  96.                     $this->redirect($strRequest);
  97.                 }
  98.             }
  99.         }
  100.         // Look for a 404 page
  101.         $obj404 PageModel::find404ByPid($objRootPage->id);
  102.         // Die if there is no page at all
  103.         if (null === $obj404)
  104.         {
  105.             throw new PageNotFoundException('Page not found: ' Environment::get('uri'));
  106.         }
  107.         // Forward to another page
  108.         if ($obj404->autoforward && $obj404->jumpTo)
  109.         {
  110.             $objNextPage PageModel::findPublishedById($obj404->jumpTo);
  111.             if (null === $objNextPage)
  112.             {
  113.                 $this->log('Forward page ID "' $obj404->jumpTo '" does not exist'__METHOD__TL_ERROR);
  114.                 throw new ForwardPageNotFoundException('Forward page not found');
  115.             }
  116.             $this->redirect($objNextPage->getFrontendUrl());
  117.         }
  118.         return $obj404;
  119.     }
  120. }
  121. class_alias(PageError404::class, 'PageError404');