return 404 on non existing controller/action and refactor index.php

This commit is contained in:
Thomas Rubini 2023-01-24 10:02:41 +01:00
parent 1388fc1205
commit 1f27156004
No known key found for this signature in database
GPG Key ID: C7D287C8C1CAC373
3 changed files with 14 additions and 10 deletions

View File

@ -55,11 +55,11 @@ final class controller
public function execute()
{
if (!class_exists($this->_A_urlParts['controller'])) {
throw new ControllerException("Controller " . $this->_A_urlParts['controller'] . " is not valid.");
throw new NotFoundException("Controller " . $this->_A_urlParts['controller'] . " is not valid.");
}
if (!method_exists($this->_A_urlParts['controller'], $this->_A_urlParts['action'])) {
throw new ControllerException("Action " . $this->_A_urlParts['action'] . " of controller " .
throw new NotFoundException("Action " . $this->_A_urlParts['action'] . " of controller " .
$this->_A_urlParts['controller'] . " is not valid.");
}

View File

@ -0,0 +1,3 @@
<?php
class NotFoundException extends Exception {}

View File

@ -16,6 +16,8 @@
View::openBuffer();
$ret = Utils::RETURN_HTML;
try
{
$O_controller = new Controller($S_url, $A_postParams, $A_getParams);
@ -23,19 +25,18 @@
}
catch (ControleurException $O_exception)
{
echo ('An error occured: ' . $O_exception->getMessage());
View::openBuffer();
View::show("errors/500", $O_exception->getMessage());
}
catch (NotFoundException $O_exception)
{
View::openBuffer();
View::show("errors/404");
}
catch (HTTPSpecialCaseException $O_exception)
{
// drop old buffer
View::closeBuffer();
View::openBuffer();
View::show("errors/".$O_exception->getHTTPCode(), $O_exception->getMsg());
$content = View::closeBuffer();
View::show('html', array('body' => $content));
return;
}