Skip to main content
added 210 characters in body
Source Link
4uk4
  • 101.7k
  • 7
  • 173
  • 217

I am trying to check a condition before page load, if value exists to load page if not to redirect.

I am following this page

I have created a /mymodule/mymodule.services.yml file and a /mymodule/src/MyModuleSubscriber.php file.

In MyModuleSubscriber.php I would like to check if a value exists, if it does to load site as normal, if the value doesn't exist to perform redirect.

This is the code, but not sure how to write it correctly. Do I need to put an else statement or just leave blank?

Is this correct way to check a value exists on every page load?

class MyModuleSubscriber implements EventSubscriberInterface {

      public function MyModuleLoadcheckForRedirect(GetResponseEvent $event) {

          $path$baseUrl = \Drupal::service$event->getRequest('path.current')->getPath>getBaseUrl();

          if($path$value == null &&
             $baseURL !== '/MyModulePage') {
              $event->setResponse(new RedirectResponse($baseUrl.'/MyModulePage'));
          }
      }


  /**
   * {@inheritdoc}
   */
  static function getSubscribedEvents() {
    $events[KernelEvents::REQUEST][] = array('MyModuleLoad', 200);
    return $events;
  }
}

Edit:

      $path = \Drupal::service('path.current')->getPath();

      if($path !== '/MyModulePage') {
          $event->setResponse(new RedirectResponse('/MyModulePage'));
      }

I am trying to check a condition before page load, if value exists to load page if not to redirect.

I am following this page

I have created a /mymodule/mymodule.services.yml file and a /mymodule/src/MyModuleSubscriber.php file.

In MyModuleSubscriber.php I would like to check if a value exists, if it does to load site as normal, if the value doesn't exist to perform redirect.

This is the code, but not sure how to write it correctly. Do I need to put an else statement or just leave blank?

Is this correct way to check a value exists on every page load?

class MyModuleSubscriber implements EventSubscriberInterface {

      public function MyModuleLoad(GetResponseEvent $event) {

          $path = \Drupal::service('path.current')->getPath();

          if($path !== '/MyModulePage') {
              $event->setResponse(new RedirectResponse('/MyModulePage'));
          }
      }


  /**
   * {@inheritdoc}
   */
  static function getSubscribedEvents() {
    $events[KernelEvents::REQUEST][] = array('MyModuleLoad', 200);
    return $events;
  }
}

I am trying to check a condition before page load, if value exists to load page if not to redirect.

I am following this page

I have created a /mymodule/mymodule.services.yml file and a /mymodule/src/MyModuleSubscriber.php file.

In MyModuleSubscriber.php I would like to check if a value exists, if it does to load site as normal, if the value doesn't exist to perform redirect.

This is the code, but not sure how to write it correctly. Do I need to put an else statement or just leave blank?

Is this correct way to check a value exists on every page load?

class MyModuleSubscriber implements EventSubscriberInterface {

      public function checkForRedirect(GetResponseEvent $event) {

          $baseUrl = $event->getRequest()->getBaseUrl();

          if($value == null &&
             $baseURL !== '/MyModulePage') {
              $event->setResponse(new RedirectResponse($baseUrl.'/MyModulePage'));
          }
      }


  /**
   * {@inheritdoc}
   */
  static function getSubscribedEvents() {
    $events[KernelEvents::REQUEST][] = array('MyModuleLoad', 200);
    return $events;
  }
}

Edit:

      $path = \Drupal::service('path.current')->getPath();

      if($path !== '/MyModulePage') {
          $event->setResponse(new RedirectResponse('/MyModulePage'));
      }
deleted 46 characters in body
Source Link
May
  • 163
  • 1
  • 10

I am trying to check a condition before page load, if value exists to load page if not to redirect.

I am following this page

I have created a /mymodule/mymodule.services.yml file and a /mymodule/src/MyModuleSubscriber.php file.

In MyModuleSubscriber.php I would like to check if a value exists, if it does to load site as normal, if the value doesn't exist to perform redirect.

This is the code, but not sure how to write it correctly. Do I need to put an else statement or just leave blank?

Is this correct way to check a value exists on every page load?

class MyModuleSubscriber implements EventSubscriberInterface {

      public function checkForRedirectMyModuleLoad(GetResponseEvent $event) {

          $baseUrl$path = $event->getRequest\Drupal::service('path.current')->getBaseUrl>getPath();

          if($value == null &&
             $baseURL$path !== '/MyModulePage') {
              $event->setResponse(new RedirectResponse($baseUrl.'/MyModulePage'));
          }
      }


  /**
   * {@inheritdoc}
   */
  static function getSubscribedEvents() {
    $events[KernelEvents::REQUEST][] = array('MyModuleLoad', 200);
    return $events;
  }
}

I am trying to check a condition before page load, if value exists to load page if not to redirect.

I am following this page

I have created a /mymodule/mymodule.services.yml file and a /mymodule/src/MyModuleSubscriber.php file.

In MyModuleSubscriber.php I would like to check if a value exists, if it does to load site as normal, if the value doesn't exist to perform redirect.

This is the code, but not sure how to write it correctly. Do I need to put an else statement or just leave blank?

Is this correct way to check a value exists on every page load?

class MyModuleSubscriber implements EventSubscriberInterface {

      public function checkForRedirect(GetResponseEvent $event) {

          $baseUrl = $event->getRequest()->getBaseUrl();

          if($value == null &&
             $baseURL !== '/MyModulePage') {
              $event->setResponse(new RedirectResponse($baseUrl.'/MyModulePage'));
          }
      }


  /**
   * {@inheritdoc}
   */
  static function getSubscribedEvents() {
    $events[KernelEvents::REQUEST][] = array('MyModuleLoad', 200);
    return $events;
  }
}

I am trying to check a condition before page load, if value exists to load page if not to redirect.

I am following this page

I have created a /mymodule/mymodule.services.yml file and a /mymodule/src/MyModuleSubscriber.php file.

In MyModuleSubscriber.php I would like to check if a value exists, if it does to load site as normal, if the value doesn't exist to perform redirect.

This is the code, but not sure how to write it correctly. Do I need to put an else statement or just leave blank?

Is this correct way to check a value exists on every page load?

class MyModuleSubscriber implements EventSubscriberInterface {

      public function MyModuleLoad(GetResponseEvent $event) {

          $path = \Drupal::service('path.current')->getPath();

          if($path !== '/MyModulePage') {
              $event->setResponse(new RedirectResponse('/MyModulePage'));
          }
      }


  /**
   * {@inheritdoc}
   */
  static function getSubscribedEvents() {
    $events[KernelEvents::REQUEST][] = array('MyModuleLoad', 200);
    return $events;
  }
}
Became Hot Network Question
Source Link
May
  • 163
  • 1
  • 10

In event subscriber, how to redirect if value doesnt exist?

I am trying to check a condition before page load, if value exists to load page if not to redirect.

I am following this page

I have created a /mymodule/mymodule.services.yml file and a /mymodule/src/MyModuleSubscriber.php file.

In MyModuleSubscriber.php I would like to check if a value exists, if it does to load site as normal, if the value doesn't exist to perform redirect.

This is the code, but not sure how to write it correctly. Do I need to put an else statement or just leave blank?

Is this correct way to check a value exists on every page load?

class MyModuleSubscriber implements EventSubscriberInterface {

      public function checkForRedirect(GetResponseEvent $event) {

          $baseUrl = $event->getRequest()->getBaseUrl();

          if($value == null &&
             $baseURL !== '/MyModulePage') {
              $event->setResponse(new RedirectResponse($baseUrl.'/MyModulePage'));
          }
      }


  /**
   * {@inheritdoc}
   */
  static function getSubscribedEvents() {
    $events[KernelEvents::REQUEST][] = array('MyModuleLoad', 200);
    return $events;
  }
}