Skip to main content
Change event class from D8/D9 to D9/D10
Source Link
4uk4
  • 101.7k
  • 7
  • 173
  • 217

You can set a response early in a request event:

 public function my404Check(GetResponseEventRequestEvent $event) {

    // only return a response for a master request
    if (!$event->isMasterRequest>isMainRequest()) {
      return;
    }

    // Do some logic here to check if it should be a 404.

    $response = new Response('', Response::HTTP_NOT_FOUND);
    $event->setResponse($response);
  }

The event dispatcher checks if a response is already set and returns this response without executing any more code to build a Drupal response.

(Code updated for D9/10)

You can set a response early in a request event:

 public function my404Check(GetResponseEvent $event) {

    // only return a response for a master request
    if (!$event->isMasterRequest()) {
      return;
    }

    // Do some logic here to check if it should be a 404.

    $response = new Response('', Response::HTTP_NOT_FOUND);
    $event->setResponse($response);
  }

The event dispatcher checks if a response is already set and returns this response without executing any more code to build a Drupal response.

You can set a response early in a request event:

 public function my404Check(RequestEvent $event) {

    // only return a response for a master request
    if (!$event->isMainRequest()) {
      return;
    }

    // Do some logic here to check if it should be a 404.

    $response = new Response('', Response::HTTP_NOT_FOUND);
    $event->setResponse($response);
  }

The event dispatcher checks if a response is already set and returns this response without executing any more code to build a Drupal response.

(Code updated for D9/10)

add check for master request
Source Link
4uk4
  • 101.7k
  • 7
  • 173
  • 217

You can set a response early in a request event:

 public function my404Check(GetResponseEvent $event) { 

    // only return a response for a master request
    if (!$event->isMasterRequest()) {
      return;
    }

    // Do some logic here to check if it should be a 404. 

    $response = new Response('', Response::HTTP_NOT_FOUND);
    $event->setResponse($response);
  }

The event dispatcher checks if a response is already set and returns this response without executing any more code to build a Drupal response.

You can set a response early in a request event:

 public function my404Check(GetResponseEvent $event) {
    // Do some logic here to check if it should be a 404.
    $response = new Response('', Response::HTTP_NOT_FOUND);
    $event->setResponse($response);
  }

The event dispatcher checks if a response is already set and returns this response without executing any more code to build a Drupal response.

You can set a response early in a request event:

 public function my404Check(GetResponseEvent $event) { 

    // only return a response for a master request
    if (!$event->isMasterRequest()) {
      return;
    }

    // Do some logic here to check if it should be a 404. 

    $response = new Response('', Response::HTTP_NOT_FOUND);
    $event->setResponse($response);
  }

The event dispatcher checks if a response is already set and returns this response without executing any more code to build a Drupal response.

Source Link
4uk4
  • 101.7k
  • 7
  • 173
  • 217

You can set a response early in a request event:

 public function my404Check(GetResponseEvent $event) {
    // Do some logic here to check if it should be a 404.
    $response = new Response('', Response::HTTP_NOT_FOUND);
    $event->setResponse($response);
  }

The event dispatcher checks if a response is already set and returns this response without executing any more code to build a Drupal response.