Page request by drupal 7
2014 01 May

How a page request is processed by Drupal 7 ?

By Gaurav Goyal

You entered an URL and suddenly Drupal returns a page, how does Drupal do it ? 

Having a conceptual framework of what happens when a request is received by Drupal is helpful, so this blog provides a quick walk-through. If you want to trace it yourself, use a good debugger, and start at index.php, which is where Drupal receives most of its requests. The sequence outlined in this section may seem complex for displaying a simple web page, but it is rife with flexibility.

Let's consider an URL for reference : https://www.innoraft.com/blogs/how-page-request-processed-drupal-7

You have requested this page, now this request is recieved by webserver (nginx or apache or etc) from browser, now an intresting question how this request is reaches Drupal. Let's have a look :

1. Apache Web Server :

  1. Apache web server respects Drupal’s .htaccess file, so some PHP settings are initialized, and the URL is examined.
  2. The mod_rewrite rule in Drupal’s .htaccess file looks at the incoming URL and separates the base URL from the path. So now path for our URL is blog/how-page-request-served-drupal-7
  3. This path is assigned to a query parameter q=blog/how-page-request-served-drupal-7
  4. Now the resulting url becomes https://www.innoraft.com/blogs/how-page-request-processed-drupal-7

2. Nginx :

  1. Nginx basically search for virtual host's config file and then after processing the request send this to Drupal. Following is the primary block for request processing :
# This is our primary location block.

location / {

index index.php;

try_files $uri $uri/ @rewrite;

expires max;

}

location @rewrite {

rewrite ^/(.*)$ /index.php?q=$1;

}

     2. What this block of code does, it redirects the incoming request to drupal's index.php in the same fashion as what apache does.

So now our page URL is changed from https://www.innoraft.com/blogs/how-page-request-processed-drupal-7 => http://www.innoraft.com/index.php?q=blogs/how-page-request-processed-drupal-7

Almost all the page request coming to drupal are processed in index.php except some request like public file downloads etc. Till now Drupal has not started processing the request.

How Drupal will process the page request ?

Let's have a look on the code of index.php file :

define('DRUPAL_ROOT', getcwd());

require_once DRUPAL_ROOT . '/includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
menu_execute_active_handler();

Yes, Four (4) lines of code returns every single page on your drupal website ! That's pretty amazing. so whats going on here.

A call is made to getcwd() and output is assigned to the 'DRUPAL_ROOT' constant, basically with this drupal is telling itself where it is installed.

Using this constant Drupal loads the bootstrap.inc file required for starting all the mechanism in Drupal. The code of this file is run on each and every page request.

After loading bootstrap.inc file, drupal_bootstrap() function is called with the value of the DRUPAL_BOOTSTRAP_FULL constant passed. In short this function starts all the mechanism like session, basic configuration, database, page caching, menu, ajax etc. After starting all of the mechanisms it executes the menu_execute_active_handler() for routing it to appropriate callback function. Basically this function looks at the query parameter (blog/how-page-request-served-drupal-7) and then look for the appropriate callbacks.

So till now we have all the content related to the requested URL, now Theming layer comes into action and theme the content.

Theming involves transforming the data that has been retrieved, manipulated, or created into HTML (or XML or other output format). Drupal will use the theme which is selected by the administrator to give the web page the correct look and feel. The resulting output is then sent to the web browser (or other HTTP client).

And that is how your page request is processed....!!!!

Latest Blogs

Security

Tips to Secure your Drupal Website with Ease: Basic Security checklist

If someone were to break in your room, they would probably learn less about you than if they hacked you on the internet.

Read More

intor_react

Introduction to React JS and Components

React is a flexible JavaScript library for building user interfaces and it lets you compose complex UIs from small and isolated pieces of codes which is known as Components.

Read More

Flexbox

How to simplify the layout of your website using Flexbox?

Have you ever scratched your head while working with CSS to make some simple layouts for your website? Are you fed up of running to the UI developers for small layout designs every time?

Read More

cache_context

Drupal 8 Cache Context: An efficient way for context based caching.

We are well aware of the fact that Drupal Cache API is a remarkable feature introduced in Drupal 8.

Read More