The new URL routing facility that I described a few days ago is now in the 
trunk. Iprovides fairly powerful rewriting with very simple configuration and 
no regexes. The configuration is described below.

WARNING: this is beta-quality code. There are surely bugs in it, and the 
API/configuration will no doubt change a little. On the other hand, it's simple 
to configure, and I'd appreciate as much testing as possible.

You should see *no* change in routing behavior if you do not add a router entry 
to routes.py. Please notify me ASAP if you notice anything to the contrary.

Features:

* remove default application/controller names from URLs

* support domain<->app mapping (no visible app names)

* support language codes embedded in URLs: /app/en/ctlr/fcn/args

* handle static files, including root-based files like favicon.ico, 
automatically

* make full URL-legal character set available for args and vars 
 (This was the original driver for making the changes, since it was essentially 
impossible to
  retrofit into the existing rewrite system. The secondary goal was to address 
99% of required
  functionality while keeping configuration as close to trivial as possible.)

* app-specific routing

The new logic is selected in routes.py. The old regex logic remains available, 
though not simultaneously.

The language feature is not yet tied into web2py's existing language support, 
so for now all you get is URL conversion and support for language-specific 
subdirectories in app/static.

There's a new routing example file called router.example.py that has some 
documentation, reproduced below. As with routes.example.py, you copy it to 
routes.py and edit it for your own configuration.


#  router is a dictionary of URL routing parameters.
#
#  For each request, the effective router is the default router (below),
#  updated by the base router (if any) from routes.py,
#  updated by the relevant application-specific router (if any)
#  from applications/app/routes.py.
#
#  Optional members of base router:
#
#  default_application: default application name
#  applications: list of all recognized applications,
#       or 'ALL' to use all currently installed applications
#  map_domain: dict used to map domain names to application names
#
#  These values may be overridden by app-specific routers:
#
#  default_controller: name of default controller
#  default_function: name of default function (all controllers)
#  root_static: list of static files accessed from root
#       (mapped to the selected application's static/ directory)
#
#
#  Optional members of application-specific router:
#
#  These values override those in the base router:
#
#  default_controller
#  default_function
#  root_static
#
#  When these appear in the base router, they apply to the default application 
only:
#
#  controllers: list of valid controllers in selected app
#       or "DEFAULT" to use all controllers in the selected app plus 'static'
#       or [] to disable controller-name omission
#  languages: list of all supported languages
#  default_language
#       The language code (for example: en, it-it) optionally appears in the 
URL following
#       the application (which may be omitted). For incoming URLs, the code is 
copied to
#       request.language; for outgoing URLs it is taken from request.language.
#       If languages=[], language support is disabled.
#       The default_language, if any, is omitted from the URL.
#  check_args: set to False to suppress arg checking
#       request.raw_args always contains a list of raw args from the URL, not 
unquoted
#       request.args are the same values, unquoted
#       By default (check_args=True), args are required to match args_match.
#  acfe_match: regex for valid application, controller, function, extension 
/a/c/f.e
#  file_match: regex for valid file (used for static file names)
#  args_match: regex for valid args (see also check_args flag)
#
#
#  The built-in default router supplies default values (undefined members are 
None):
#
#     router = dict(
#         default_application = 'init',
#             applications = 'ALL',
#         default_controller = 'default',
#             controllers = 'DEFAULT',
#         default_function = 'index',
#         root_static = ['favicon.ico', 'robots.txt'],
#         map_domain = dict(),
#         languages = [],
#         default_language = None,
#         check_args = True,
#         map_hyphen = True,
#         acfe_match = r'\w+$',              # legal app/ctlr/fcn/ext
#         file_match = r'(\w+[-=./]?)+$',    # legal file (path) name
#         args_match = r'([\w@ -]+[=.]?)+$', # legal arg in args
#     )
#
#  See rewrite.map_url_in() and rewrite.map_url_out() for implementation 
details.


#  This simple router overrides only the default application name,
#  but provides full rewrite functionality.
#
#  router = dict(
#     default_application = 'welcome',
#  )

#  This router supports the doctests below; it's not very realistic.
#
router = dict(
    applications = ['welcome', 'admin', 'app', 'myapp', 'bad!app'],
    default_application = 'myapp',
    controllers = ['myctlr', 'ctr'],
    default_controller = 'myctlr',
    default_function = 'myfunc',
    languages = ['en', 'it', 'it-it'],
    default_language = 'en',
    map_domain = {
        "domain1.com" : "app1",
        "domain2.com" : "app2"
    },
)

Reply via email to