Skip to main content

Error Case

On this page

Invalid requests from the user have to be handled by responding with an appropriate error page.

Changing the HTTP Status​

If you want to output an error page, you can change the HTTP status via n2n\web\http\Response:setStatus().

class ExampleController extends ControllerAdapter {

// some methods

public function notFound() {
$this->getResponse()->setStatus(Response::STATUS_404_NOT_FOUND);

$this->forward('view\notFound.html');
}
}

StatusException​

In the event of an error, however, it is usually more sensible and also easier to throw an n2n\web\http\StatusException. In this case the n2n exception handler outputs a configurable error page with the corresponding status code. You can pass any status code to this exception in the constructor: new StatusException(Response::STATUS_404_NOT_FOUND);

For the most common error cases, however, exceptions are already prepared that no longer require a status code to be passed:

  • 404: n2n\web\http\PageNotFoundException
  • 403: n2n\web\http\ForbiddenException

The following example shows how you could apply an n2n\web\http\PageNotFoundException:

public function doDetail($id, ArticleDao $articleDao) {
$article = $articleDao->getArticleById($id);
if ($article === null) {
throw new PageNotFoundException();
}

$this->forward('view\detail.html', array('article' => $article));
}

Custom Status Page​

If the n2n exception handler catches a HttpStatusException, it outputs the view n2n/view/errorpages/status.html by default. In the app.ini under the group [error] you can register a separate view for each HTTP status:

[error]

error_view.default = "atusch\view\status.html"
error_view.403 = "atusch\view\forbidden.html"
error_view.404 = "atusch\view\notFound.html"

If no view is defined for a status, error_view.default is used.