URL rewriting

1 view
Skip to first unread message

Gus Prevas

unread,
Jan 24, 2011, 9:20:31 PM1/24/11
to ronin...@googlegroups.com
Hi all,

I wanted to follow up on the discussion from a few weeks ago on URL mapping/rewriting.

I've just added a highly flexible configuration point for those who wish to change how Ronin does URL mapping; it's checked in to the source tree and will be available in the next release.

As an example, here's some sample code which does what Brendan was asking for: allows you to specify an alternate name for a controller class via an annotation.  Adding the following code in the constructor of config.RoninConfig will do the trick (with "uses" statements excluded for brevity):

    URLHandler = new DefaultURLHandler() {
      override function getControllerMethod(request : String[]) : IMethodInfo {
        if(request.length > 0) {
          for(controllerName in TypeSystem.getAllTypeNames()
            .where(\t -> t.length() > 11 and t.subSequence(0, 11).equals("controller."))) {
            var controllerType = TypeSystem.getByFullName(controllerName as String)
            var annotation = controllerType.TypeInfo.getAnnotation(Controller)
            if(annotation != null) {
              if(request[0] == (annotation.Instance as Controller).PathName) {
                request[0] = controllerType.RelativeName
                break
              }
            }
          }
        }
        return super.getControllerMethod(request)
      }
    }

This code assumes that your annotation is named "Controller", and its argument is available via the property Controller#PathName.  I've tested this code superficially to confirm that it works.

If you were going to use this code in a real system, there's a few changes you'd want to make:

- Replace the anonymous inner class with a real top-level class.
- Cache the controller type names instead of looping through every type name in the type system on every request.
- Probably use a cache that maps path names to controller classes so that every hit to a path name after the first was a simple lookup.

Hope this helps!
-Gus
Reply all
Reply to author
Forward
0 new messages