0

Project Documentation says.. Both are invoked even for cached page views. hook_boot says

This hook is run at the beginning of the page request. It is typically used to set up global parameters that are needed later in the request.

Only use this hook if your code must run even for cached page views. This hook is called before the theme, modules, or most include files are loaded into memory. It happens while Drupal is still in bootstrap mode.

hook_exit says...

Perform cleanup tasks.

This hook is run at the end of most regular page requests. It is often used for page logging and specialized cleanup. This hook MUST NOT print anything because by the time it runs the response is already sent to the browser.

Only use this hook if your code must run even for cached page views. If you have code which must run once on all non-cached pages, use hook_init() instead. That is the usual case. If you implement this hook and see an error like 'Call to undefined function', it is likely that you are depending on the presence of a module which has not been loaded yet. It is not loaded because Drupal is still in bootstrap mode.

Interested to know during which bootstrap phases hook_boot & hook_exit getting invoked so that it will be helpful in cases where above hooks can be used....

1

hook_boot is getting invoked during fifth phase which is DRUPAL_BOOTSTRAP_PAGE_HEADER which set up the page header... This is for non cached pages..

/**
 * Invokes hook_boot(), initializes locking system, and sends HTTP headers.
 */
function _drupal_bootstrap_page_header() {
  bootstrap_invoke_all('boot');

  if (!drupal_is_cli()) {
    ob_start();
    drupal_page_header();
  }
}

For Cached pages both gets executed at Second Phase of Bootstrap which is DRUPAL_BOOTSTRAP_PAGE_CACHE with little difference...

  // If the skipping of the bootstrap hooks is not enforced, call
  // hook_boot.
  if (variable_get('page_cache_invoke_hooks', TRUE)) {
    bootstrap_invoke_all('boot');
  }
  drupal_serve_page_from_cache($cache);
  // If the skipping of the bootstrap hooks is not enforced, call
  // hook_exit.
  if (variable_get('page_cache_invoke_hooks', TRUE)) {
    bootstrap_invoke_all('exit');
  }

what make difference between these hooks is

  drupal_serve_page_from_cache($cache);

Which tells hook_boot and hook_exit is used before and after page requests for tasks like setup and cleanup respectively..

hook_exit also called in common.inc what makes difference is, Invoke during DRUPAL_BOOTSTRAP_PAGE_CACHE phase applies where cached page requests are served.. In common.inc hook_exit is called for non cached pages..

function drupal_page_footer() {
  global $user;

  module_invoke_all('exit');

  // Commit the user session, if needed.
  drupal_session_commit();

  if (variable_get('cache', 0) && ($cache = drupal_page_set_cache())) {
    drupal_serve_page_from_cache($cache);
  }
  else {
    ob_flush();
  }

  _registry_check_code(REGISTRY_WRITE_LOOKUP_CACHE);
  drupal_cache_system_paths();
  module_implements_write_cache();
  system_run_automated_cron();
}

Above confirms even for non cached requests hook_exit is invoked..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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