I have a custom controller with a parametric route:

  path: '/path/{a_id}/{b_id}/greeting'
    _controller: '\Drupal\my_module\Controller\GreetingController::greetingShow'

How can I display the normal 404 page, if a_id or b_id are not found in the database?

class GreetingController extends ControllerBase {

  public function greetingShow($a_id, $b_id) {

    $a = AUtils::getAById($a_id);

    if(!$a || $b_id !== $a->get('uid')->getString() || $a->get('x')->getString() !== '1') {
      // just show the standard 404, but how?

    return [];

1 Answer 1


You just throw an exception anywhere in your controller and this is caught by Drupal to build a themed error page:

use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;


  if (!$a || $b_id !== $a->get('uid')->getString() || $a->get('x')->getString() !== '1') {
    throw new NotFoundHttpException();
  • Works like a charm, thx a lot!
    – Andreas
    Jul 25, 2022 at 15:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.