$_SERVER['REQUEST_URI'] in Zope

Adam Alton - September 11th, 2009

Having a head that contains a PHP-shaped hammer I have a tendency to look for PHP-shaped nails when trying to make stuff.  So I’ve always been annoyed at not being able to find the equivalent of $_SERVER['REQUEST_URI'] in Zope.

Then the other day, when Rich was tweaking (read:fixing) the app that I built as my training project when I started at Rubber Towers he stuck in this:

x = self.REQUEST.ACTUAL_URL

Bingo!

It gives you the actual url that was requested by the browser, query string and all.  Sweet.

Tags:

6 Responses to “$_SERVER['REQUEST_URI'] in Zope”

  1. Chris Rossi says:

    Stories like this are further evidence why Zope/Plone really, really needs comprehensive reference documentation. It’s cool that you finally found the answer via Plone’s rich oral tradition, but how much time (and money) was wasted in the meantime?

  2. mmm, not quite. ACTUAL_URL does not contain a query string. For that, you have to concatenate ACTUAL_URL and QUERY_STRING. The purpose of ACTUAL_URL is to have a reliable way to get the URL the user sees, even when virtual hosting is being used.

    I’d also note that it’s better to use something like self.request['ACTUAL_URL'] instead of attribute notation. Or at least, it’s in better taste. :)

    Martin

  3. KC says:

    +1 for Chris Rossi

  4. claytron says:

    There are some util view classes in plone.app.layout.globals that do just this. Take a look at the context.py and you’ll see you can do the following:

    p_context_state = getMultiAdapter((self.context, self.request), name=u”plone_context_state”)

    # to get the actual url
    p_context_state.current_base_url()

    # to get the url + the query
    p_context_state.current_page_url()

  5. AdamA says:

    +1 for documentation.

    Thanks Martin, funnily enough this morning I need this very thing, so your comment is quite useful.

    claytron, is there actually any advantage in looking up a multi adapter just to get the requested url? My current use case is in a template so that’s not really practical and seems a bit overkill for just concatenating two strings together.

    It still annoys me that you have to join the two bits together with ‘?’, who votes for adding REQUEST['REQUEST_URI'] to Zope?

Leave a Reply