image
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 : http://www.innoraft.com/blog/how-page-request-served-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 http://www.innoraft.com/index.php?q=blog/how-page-request-served-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 http://www.innoraft.com/blog/how-page-request-served-drupal-7 => http://www.innoraft.com/index.php?q=blog/how-page-request-served-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....!!!!

Featured blog

web-personalization

Personalized Content is a Proven Entity !!

Irrespective of how big a business icon or brand you are, increasing the relevance of your website will always be critical to your success.

Read More

Git Hooks

Git hooks for better codes

We are programmers and we are always on the lookout for ways to improve our code. A good and structured way of coding defines the completeness of a programmer.

Read More

Drupal ,varnish cache

Hard time with Drupal, Varnish Cache and Cookies

Using a reverse proxy server in front of a web server is usually needed for every big site and it is a very good thing to do so as reverse proxy server will handle all the anonymous traff

Read More

Say no to captcha

Say no to captcha - Various Spam Protection Methods

Maintaining high traffic websites have their own merits and demerits, the most annoying thing about them is SPAM.

Read More