Tuesday, 3 July 2012

Drupal as framework pt2 where is my front controller?

Introduction

Continuing thoughts on Drupal7 (and to be fair probably just an exercise in tidying up my brain and not much use to others) as a web development framework, I highlighted one of the major difficulties in part 1 in that you need to know a lot about Drupal before you can use it as a framework.
This post is a quick pointer if you come from a pure framework background and are trying to understand how Drupal 7 works.

Please don't think I dislike Drupal

Some people get uncomfortable by anything but bouncy happy optimism, personally if the glass is half full I want to know how easy is it to get a refill. I will post about why I invest so much time in Drupal later.

Where is the controller?

Typically you may start looking for some sort of controller, some PHP code somewhere, that tells you how urls are mapped and dealt with. In many frameworks you can follow this chain through the source code and then start experimenting (break it to understand it). There are many ways to describe it and disagreements over details and terminology but something akin to a Front Controller is what you may expect to find. Looking for something like this is not a good initial approach to take with Drupal 7
In Drupal page requests are handled by index.php which contains just the following code:
<?php

/**
 * @file
 * The PHP page that serves all page requests on a Drupal installation.
 *
 * The routines here dispatch control to the appropriate handler, which then
 * prints the appropriate page.
 *
 * All Drupal code is released under the GNU General Public License.
 * See COPYRIGHT.txt and LICENSE.txt.
 */

/**
 * Root directory of Drupal installation.
 */
define('DRUPAL_ROOT', getcwd());

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

The controller here is menu_execute_active_handler() the problem if you want to follow what happens afterwards is that there is no extended object or xml configuration or any sort of centralized user generated code file that you can find to understand how urls are mapped to actions (page callback functions). This function is a core function that ultimately gets its appropriate page callback function to execute from a database table called menu_router obtains its content from the action, allows any interested modules to hook in and add further content and finally renders the content to the browser (by default). The menu_router table will be mostly populated from a disparate collection of modules (both core, contributed and custom), implementing hook_menu.
My advice if you are trying to quickly get a handle on Drupal as a framework, is to understand hook_menu when I first started using Drupal the name put me off for a bit, something to do with menus? doesn't sound as important as it is but to quote from the documentation.
This hook enables modules to register paths in order to define how URL requests are handled.
more on hooks and the framework in a further post.
The following from DrupalconSF 2010 is a nice introduction to how content gets rendered in Drupal, Although bear in mind that some of the exact details may differ as it was quite early in Drupal 7 development.

If you want to see a built page just before it gets rendered then you can run a simple php script using drush scr to boostrap your application and run it.
<?php

$path = drupal_lookup_path('source','your/path');
echo "$path\n";

$page_result = menu_execute_active_handler($path,False);

print_r($page_result);
echo "\n";


What you will get should be the very large(usually) render array just before it is rendered in the to html (or possible something else). The drupal_lookup_path is required as the path you see is probably not the path Drupal uses due to path aliases and clean urls.

13 comments:

Anonymous said...

Your style is so unique compared to other folks I have read stuff from. Thank you for doing this.
Free Background Check or [url=http://www.freebackgroundcheck.info ] Free Background Check[/url]

Anonymous said...

white pine dating http://loveepicentre.com/articles.php dating japan guide

Anonymous said...

ebook readers that can underline text http://audiobooksworld.co.uk/de/Sarah-Moore/m114709/ charles atlas ebook downloads [url=http://audiobooksworld.co.uk/Management-and-Leadership/c1254/?page=5]droid ebook reader app reviews[/url] feeling good handbook ebook

Anonymous said...

writing software http://buyoem.co.uk/product-36574/Autodesk-Mudbox-2012-SP1-Multi-x32 bible logo series software x [url=http://buyoem.co.uk/es/product-12172/Adobe-Creative-Suite-3-Web-Premium-Mac]suntouch software[/url] computer hacker protection software reveiws
[url=http://buyoem.co.uk/es/product-34251/Corel-Designer-Technical-Suite-X5-15-2]Corel Designer Technical Suite X5 15.2 - Cheap Legal OEM Software, Software Sale, Download OEM[/url] open source software for vista
[url=http://buyoem.co.uk/fr/category-100-103/Graphiques-et-Publication?page=5][img]http://buyoem.co.uk/image/1.gif[/img][/url]

Anonymous said...

svd rip software http://buyoemsoftware.co.uk/product-37281/TreePad-X-7-1-Enterprise telarc dvd decryption software [url=http://buyoemsoftware.co.uk/de/manufacturer-1/Corel]menu planning software cook[/url] medical software system
[url=http://buyoemsoftware.co.uk/product-35373/3herosoft-iPod-Video-Converter-3-4-MacOSX]3herosoft iPod Video Converter 3.4 MacOSX - Download OEM, Software Sale, OEM Software[/url] wifi snffer software all network types
[url=http://buyoemsoftware.co.uk/it/product-14632/AppleWorks-6-2-Mac][img]http://buyoem.co.uk/image/5.gif[/img][/url]

Anonymous said...

sim card hacking software http://buyoemsoftware.co.uk/es/product-35454/Odin-HDD-Encryption-5-5 genealogy software family tree maker mac [url=http://buyoemsoftware.co.uk/category-100-104/Software-Plugins?page=16]tax schedule d calculation software[/url] best freeware ftp software
[url=http://buyoemsoftware.co.uk/product-36750/Argus-Monitor-2-0]Argus Monitor 2.0 - Software Store[/url] windows laptop dj software
[url=http://buyoemsoftware.co.uk/product-37059/Adrosoft-Steady-Recorder-2-7][img]http://buyoem.co.uk/image/1.gif[/img][/url]

Anonymous said...

sharepod software http://buyoemsoftware.co.uk/de/product-34597/Adobe-Creative-Suite-5-5-Production-Premium-MAC dvd decoder software windows me [url=http://buyoemsoftware.co.uk/fr/product-16330/MemTest-4-2-Mac]best webcam software for low bandwidth[/url] word prediction software efficacy studies
[url=http://buyoemsoftware.co.uk/product-33531/Artizen-Natural-3-0-for-Photoshop-x32]Artizen Natural 3.0 for Photoshop x32 - Software Store[/url] grade schedule software
[url=http://buyoemsoftware.co.uk/de/product-35375/3herosoft-MP4-to-DVD-Burner-3-5-MacOSX][img]http://buyoem.co.uk/image/4.gif[/img][/url]

Anonymous said...

tapi fax software free relieve http://buysoftwareonline.co.uk/manufacturer-8/Other blood sugar software [url=http://buysoftwareonline.co.uk/category-12/Multimedia-and-Entertainment?page=125]surveilence web cam software[/url] what is software engineering process
[url=http://buysoftwareonline.co.uk/category-100-102/Business?page=14]Business - Software Store[/url] windows video messaging software
[url=http://buysoftwareonline.co.uk/it/product-14870/SpaceObServer-3-0][img]http://buyoem.co.uk/image/5.gif[/img][/url]

Anonymous said...

[url=http://redbrickstore.co.uk/products/albendazole.htm][img]http://onlinemedistore.com/2.jpg[/img][/url]
clarks pharmacy http://redbrickstore.co.uk/products/cordarone.htm gatti pharmacy in indiana pa [url=http://redbrickstore.co.uk/products/diamox.htm]starting salary for pharmacy degree[/url]
mark bonfiglio and pharmacy http://redbrickstore.co.uk/products/lopid.htm online pharmacy albuterol [url=http://redbrickstore.co.uk/products/capoten.htm]capoten[/url]
pharmacy care alliance http://redbrickstore.co.uk/products/dilantin.htm average community pharmacy fills [url=http://redbrickstore.co.uk/products/methotrexate.htm]pharmacy school in orlando florida[/url]
sample resumes for pharmacy technicians http://redbrickstore.co.uk/products/flonase.htm offshore pharmacy ezetimibe [url=http://redbrickstore.co.uk/products/augmentin.htm]augmentin[/url]

Anonymous said...

Exсellent bеаt ! I wish tο appгentice while уou amend yοur sitе, how сοuld i subscribе for a blοg website?
The account helped me a aссеptable ԁeal.
I had been tiny bit acquaіnted of thiѕ your broaԁcaѕt оffеred bгight cleаг іdea

Tаke а lοoκ at my ωеblοg - raspberry ketone

Anonymous said...

[url=http://certifiedpharmacy.co.uk/products/gift-kamagra-jelly.htm][img]http://onlinemedistore.com/10.jpg[/img][/url]
pharmacy schools in pennsylvainia http://certifiedpharmacy.co.uk/categories/skin-care.htm texas state pharmacy certificate [url=http://certifiedpharmacy.co.uk/products/vasotec.htm]oklahoma pharmacy board[/url]
new pharmacy program http://certifiedpharmacy.co.uk/products/lexapro.htm compounding pharmacy bloomington il [url=http://certifiedpharmacy.co.uk/products/kamagra.htm]kamagra[/url]
meadowbrook pharmacy charlottesville http://certifiedpharmacy.co.uk/categories/female-enhancement.htm texas state pharmacy tech [url=http://certifiedpharmacy.co.uk/products/lamictal.htm]mid atlantic pharmacy[/url]
cephalexin online pharmacy http://certifiedpharmacy.co.uk/products/aricept.htm natural health products pharmacy [url=http://certifiedpharmacy.co.uk/products/lopid.htm]lopid[/url]

Anonymous said...

[url=http://englandpharmacy.co.uk/products/atacand.htm][img]http://onlinemedistore.com/10.jpg[/img][/url]
healthy pet pharmacy http://englandpharmacy.co.uk/products/noroxin.htm board of pharmacy california [url=http://englandpharmacy.co.uk/products/noroxin.htm]david mclean and pharmacy[/url]
guardian pharmacy centro warwick perth wa casual job http://englandpharmacy.co.uk/products/lithium-carbonate.htm glucophage online pharmacy [url=http://englandpharmacy.co.uk/products/augmentin.htm]augmentin[/url]
setzer pharmacy http://englandpharmacy.co.uk/products/digoxin.htm pharmacy in thailand [url=http://englandpharmacy.co.uk/products/copegus.htm]pharmacy technical help contacts[/url]
pharmacy ground glass bottle http://englandpharmacy.co.uk/products/vermox.htm compare wisconsin pharmacy [url=http://englandpharmacy.co.uk/products/pilocarpine.htm]pilocarpine[/url]

Anonymous said...

dating mentally abused women http://loveepicentre.com men dating young girls
swedish dating service [url=http://loveepicentre.com/success_stories/]100 free new online dating website[/url] lesbian onlin dating service
interracial dating website match [url=http://loveepicentre.com/map/]dating japan pen friends for[/url] oprah winfrey dating stedman gail [url=http://loveepicentre.com/user/eva1212/]eva1212[/url] modern day dating do's and don'ts