I'm interested in different forms of url's and had a read about
routes.py.  But the manual tells me my app shouldn't rely on routes.py
being present.  So I thought of the following approach  (it's the
result of all of 15 minutes thinking/code/test so don't expect too
much).

* I want to be able to recognise http://myserver/myapp/anytable/an_optional_id

* let's assume I have a table 'user' and a controller 'default.py'
that contains a function 'default()'

* in compileapp.py the code identifies (or not) the controller and
function and outputs 'invalid controller' or 'invalid function' as
appropriate

* my change  basically says: if you don't find the controller try
again with a default value 'default' before failing and if you don't
find the function try again with the value 'default' before failing

* in addition, each time it defaults it pushes the lower element to
args[]

* so when I enter http://myserver/myapp/user/1 control is passed to
the default() function of the default.py controller and the two args
are 'user' and '1'

The code changes in compileapp.py start at line 190 and are marked
below (new stuff):

    else:
        filename=os.path.join(folder,'controllers/%s.py' % controller)
        if not os.path.exists(filename):
            # new stuff
            default_controller='default'
            filename = os.path.join(folder,'controllers/%s.py' %
default_controller)
            if os.path.exists(filename):
                request=environment['request']
                request.args.insert(0,function)
                request.function=request.controller
                request.controller=default_controller
            else:
            # end of new stuff
                raise HTTP(400,error_message_custom % 'invalid
controller',
                           web2py_error='invalid controller')
        code=open(filename,'r').read()
        exposed=regex_expose.findall(code)
        if not function in exposed:
            # new stuff
            default_function='default'
            function=default_function
            if function in exposed:
                request=environment['request']
                request.args.insert(0,request.function)
                request.function=default_function
            else:
            # end of new stuff
                raise HTTP(400,error_message_custom % 'invalid
function',
                           web2py_error='invalid function')

Enjoy!
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"web2py Web Framework" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/web2py?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to