Error handling

With the product list and product pages in place, we will now complete the functionality with some error handling.

Try to navigate to a page for a non-existing product, e.g. http://localhost/products/baz

Depending on your PHP-settings, the page will display an error or just a product page with no product content.

The NotFoundResult

The desired result is to show a Not Found-page when the product does not exist.

Open Application/Controllers/ProductsController.php and modify the content to look like this:

<?php declare(strict_types=1); namespace Application\Controllers; use Application\Models\Product; use BlueMvc\Core\ActionResults\NotFoundResult; use BlueMvc\Core\Controller; use BlueMvc\Core\View; class ProductsController extends Controller { public function indexAction(): View { $products = Product::getAll(); $this->setViewItem('Title', 'Products'); return new View($products); } public function defaultAction($action): View|NotFoundResult { $product = Product::getById($action); if ($product === null) { return new NotFoundResult(); } $this->setViewItem('Title', $product->Name); return new View($product); } }

Check the result

Reload the http://localhost/products/baz page to see the new result:

The exact result may vary depending on the browser used, but the standard "Not Found" page should be displayed.

Page not found

As we can see, when returning a NotFoundResult from a controller, a 404 Not Found result will be returned to the browser.

The NotFoundResult is a result of type action result that can be returned from a controller. It's very easy to create a custom action result.

Customize the error page

As nice as the browsers default error page might be, we will now add a final touch and create a custom error page that will be displayed whenever a page is not found.

This is done by extending a special controller, called the error controller.

The error controller

Create a new file ErrorController.php in the directory Application/Controllers/ with the following content:

<?php declare(strict_types=1); namespace Application\Controllers; use BlueMvc\Core\View; class ErrorController extends \BlueMvc\Core\ErrorController { public function _404Action(): View { return new View(); } }

Since PHP doesn't allow methods to start with a digit, any action method starting with a digit must be prepended with an underscore.

The error view

Create the directory Application/Views/Error/ and in that directory, add the file 404.twig with the following content:

{% extends "sitelayout.twig" %} {% block content %} <h1>Not Found</h1> <p> The requested page could not be found. </p> {% endblock %}

Add error controller to setup

To use the new error controller, it must be activated in the application setup.

Open Application/Setup/ApplicationSetup.php and modify the content to look like this:

<?php declare(strict_types=1); namespace Application\Setup; use Application\Controllers\ErrorController; use Application\Controllers\IndexController; use Application\Controllers\ProductsController; use BlueMvc\Core\Interfaces\ApplicationInterface; use BlueMvc\Core\Route; use BlueMvc\Twig\TwigViewRenderer; use DataTypes\System\FilePath; /** * The application setup. */ class ApplicationSetup { /** * Sets up the application. * * @param ApplicationInterface $application The application. */ public static function setup(ApplicationInterface $application): void { // Set up view handling. $application->addViewRenderer(new TwigViewRenderer()); $application->setViewPath(FilePath::parseAsDirectory('../Application/Views')); // Set up routes. $application->addRoute(new Route('', IndexController::class)); $application->addRoute(new Route('products', ProductsController::class)); $application->setErrorControllerClass(ErrorController::class); } }

Check the result

Reload the http://localhost/products/baz page to see the new result:

Page not found


How it works

Whenever a response contains an error (i.e. status code 4xx or 5xx) and an error controller is set via setErrorControllerClass in Application, BlueMvc will try to invoke a suitable action in the error controller. The action in this case is the same as the status code.

That concludes the product list part of this tutorial for now.

In the next part we will create a simple form with the bluemvc-forms package.

✎ Published in category get started