MODX Shortnames
I remember the days when I used to charge differently depending on the number of pages a site would end up eventually being. A basic site included 5 pages and maybe a contact form if you were lucky. Now that I'm building sites that have hundreds of pages, I have to do things differently, including thinking about the organization of the various pages. But when pages are buried, they're hard to find. So, I set out to write my first MODX plugin.
The problem started when I was developing a new site for The Salvation Army of the Carolinas using MODX Evolution. At the time of writing, the site has close to 1200 pages with dozens of users and representing over 50 of The Salvation Army's various local offices. All of this content requires a pretty intense information architecture so visitors (and MODX users) don't get lost when they're looking for information. MODX of course has always made it easy to organize information - letting me switch parents on the fly, or duplicating entire "folders" of content. But the flipside of all of this organization is that now we have very long (yet admittedly very logical) URIs.
For example, say I wanted to get to one of The SA's local office's website - let's say High Point, NC. Well, of course I'll start with my base url (http://www.salvationarmycarolinas) and then to keep all of my local offices organized, I enter the "command" folder and finally the High Point homepage. So, I end up with this:
http://www.salvationarmycarolinas.org/commands/highpoint
It's logical, but not exactly the kind of thing that's incredibly easy to remember. The same is true of some of our features (http://www.salvationarmycarolinas.org/features/japan) and many of or programs.
Sometimes, it just happens that content that is logically filed away in a folder structure needs to be accessed quickly and easily. What I wanted was to be able to enter a simple alias and have the site resolve to the correct page. At first I thought I'll just create "weblink" resources to point to the correct folder structure. But as I started to plan this out in my head, I had to make too many concessions. What if someone typed http://www.salvationarmycarolinas.org/high-point.... in that case I would need two "weblink" resources for one office. It just felt too cumbersome.
Then I started learning about the joy that is the MODX plugin: code that I can call following any number of built-in events in the MODX system. There are a whole list of these events: "onDocPublished","onCacheUpdate","onSiteRefresh", and the one that would prove to be the key for me.... "onPageNotFound"
With this event, I can run some PHP if it happens that a page can't be found. In my site structure, there is no resource with the address http://www.salvationarmycarolinas.org/high-point and if anyone tried to visit that address, normally they'd get a 404 error. But now, before MODX delivers the 404 I can step in and say, "Not So Fast, MODX." Using PHP, I check a simple DB table that has a "shortname" field and a "resource id" column. If there is an entry for "high-point" I grab the associated resource id, have modx make a url and redirect to the new page. If there's no record in my short names table, then MODX delivers the 404 error.
It was an amazingly simply solution to build - but it's been incredibly effective. I can keep all of my content organized just the way I want it, while still allowing our supporters and visitors easy access without having to remember a lengthy url.
Case in point: the response to the Japan Earthquake. I set up the shortname "japan" and tied it to a resource id. Now I can literally put that page anywhere that it makes sense in my site structure but users won't have to remember http://www.salvationarmycarolinas.org/long/urls/are/horrible/japan.... just http://www.salvationarmycarolinas.org/japan. I can move the resource around - into an archive, for example, and the short name will still resolve correctly.
Yet another fantastic reason to choose MODX.
Aside: I haven't replicated this with revolution, but my thought is that it would be at least as easy if not easier. We're getting ready to start in on moving to Revolution soon so I'll keep you up to date.


Comments (1)
@mrhaw:
Mar 18, 2011 at 03:17 PM
I love on "onPageNotFound". When people report a youtube video isn't working no more on my site this is the event I'm using. :)
This thread has been closed from taking new comments.