Routing and Template Cookbook: Find the handler for a URL

From Dreamwidth Notes
Jump to: navigation, search

Finding pages with BML was very easy--it mapped to the filesystem in the htdocs folder. Finding where a URL is handled with routing is a bit trickier if you don't know what controller it is using. Fortunately, there is a script that can tell you this. To see the help page, you can use:

$LJHOME/bin/dev/lookup-routing --help --verbose 

URL Path => Controller mapping

Let's say you want to look up the new entry post page. You find the URL of the page, and take everything after the domain name and use it as input to lookup-routing:

$LJHOME/bin/dev/lookup-routing /entry/new

Look up routing then returns these results:

app: /entry/new is defined in cgi-bin/DW/Controller/Entry.pm ( ln 58 ), 
   and using the handler sub new_handler
user: /entry/new is defined in cgi-bin/DW/Controller/Entry.pm ( ln 70 ), 
   and using the handler sub _new_handler_userspace

The first entry for app means that in application space (like www.dreamwidth.org), you can find the handler for the view in the controller whose file is "cgi-bin/DW/Controller/Entry.pm". The subroutine is called "new_handler" and it is on line 58 of the file.

The second entry for user means that in user space (like USERNAME.dreamwidth.org), you can find the handler for the view in the controller whose file is "cgi-bin/DW/Controller/Entry.pm". The subroutine is called "new_handler_userspace" and is on line 70 of the file.

More detailed information can be obtained using the --verbose switch. Here is an an example using a navigation menu URL:

 $LJHOME/bin/dev/lookup-routing --verbose /nav/create

Results:

app: /nav/create is defined in cgi-bin/DW/Controller/Nav.pm ( ln 29 ), and using the handler sub nav_handler
 * Default Format: html
 * Enabled for formats: html, json
 * Enabled For: app
 * Regex: (?^:^/nav(?:/([a-z]*))?$)
 * Matched Subpatterns:
      'create'

Here are some things to notice about that output

  • The URL is defined in app space, so applies only on www.dreamwidth.org
  • The handle is in the file cgi-bin/DW/Controller/Nav.pm using handler sub nav_handler defined on line 29
  • The default output format is html
  • The valid formats are html and json. If you go to the URL and add .json at the end, it will return JSON as output.
  • The URL we gave has matched a regular expression. The regular expression means that the "create" part of the URL is a matched subpattern and given as an argument to the handler.

List of contents of the routing table

If you want to list all URL patterns that are defined, you can use this

 $LJHOME/bin/dev/lookup-routing --list # everything in the routing table

Results:

 app: /__rpc_cuttag is defined in the file cgi-bin/DW/Controller/RPC/CutExpander.pm,
    and using the handler sub cutexpander_handler
 app: /about is a controller-less template: views/misc/about.tt
   * Defined in cgi-bin/DW/Controller/Dreamwidth/Misc.pm
 app: /admin is a redirect to /admin/
 app: /admin/ is defined in the file cgi-bin/DW/Controller/Admin.pm, and using the
    handler sub admin_handler
 ...
 app: /post/(\d+) is defined in the file cgi-bin/DW/Controller/Post.pm, and using the
     handler sub edit_handler
 ...

You can also use some filters to limit the URLs given. This example would only list regular expression definitions in user space:

 # filter to just those that use regex matching and are in user-space
 # (e.g., http://$username.dreamwidth.org/ vs. 
 lookup-routing --list --regex --user http://www.dreamwidth.org)

Summary stats

There is also a general routing stats report:

 $LJHOME/bin/dev/lookup-routing --stats

Results:

 String routing table:
     48 - app
      5 - user
     53 - TOTAL
 Regex routing table:
      4 - app
      1 - user
      5 - TOTAL
 Total entries: 58