In eigenem Modul auf 404 Seite weiterleiten
In einem eigenen Modul möchte ich gerne auf die in der Seitenstruktur verfügbare 404 Seite leiten, sofern verfügbar. Wie würde man da idealerweise vorgehen? Momentan mache ich es so:
PHP-Code:
// find the matching root page
$objRootPage = $this->getRootPageFromUrl();
// look for a 404 page
$obj404 = \PageModel::find404ByPid($objRootPage->id);
// die if there is no page at all
if ($obj404 === null)
{
header('HTTP/1.1 404 Not Found');
die_nicely('be_no_page', 'Page not found');
}
// redirect to 404 page
$this->redirect($this->generateFrontendUrl($obj404->row(), null, $objRootPage->language), (($obj404->redirect == 'temporary') ? 302 : 301));
(tlw. aus PageError404 kopiert). Oder gäbe es einen einfacheren Weg, über eine API Funktion die ich noch nicht kenne bspw.?
Page Not Found in Contao 4.x
Nur falls nochmal jemand auf diesen Thread stößt:
In Contao 4 gibt es eine eigene Exception, die zu einer vorhandenen Seite des Typs "404 Seite nicht gefunden" führt:
PHP-Code:
use Contao\CoreBundle\Exception\PageNotFoundException;
throw new PageNotFoundException('Page not found: ' . Environment::get('uri'));
Falls in Contao die 404-Seite nicht angelegt ist, wird das Symfony-Template vendor/symfony/twig-bundle/Resources/views/Exception/error.html.twig angezeigt…