route to controllers: Some idea may take from ASP.NET MVC's design

42 views
Skip to first unread message

maverick

unread,
Jun 3, 2008, 7:32:36 PM6/3/08
to web2py Web Framework
I am learning web2py and very new to Python, so I could be wrong or I
am not yet understand Massimo Di Pierro's principles, just some
opinions for discuss.

Looks like the URL rules /[applicaiton]/[controller]/[function] is
hard coded, I saw the code in main.py (lines near 140) :

request.application=items[0]
request.controller=items[1]
request.function=items[2]

And also I found /static/... is hard coded as well.

Yes, with rewrite.py(route.py), we can rewrite any URL in this format
and rewrite any this kind of URL to whatever we want. However there
maybe some design which could be more flexible and yet keep CoC in
good shape.

Look at the latest Microsoft ASP.NET MVC (now in preview 3, you can
read this blog http://weblogs.asp.net/scottgu/archive/2008/05/27/asp-net-mvc-preview-3-release.aspx
to get some general idea), it also have a route mapping mechanism, you
can write your rule to define which part means "controller" which part
means "action", if you read the blog post I give, in the end part it
introduced the route.

Something interesting is, that new added route part is NOT a part of
ASP.NET MVC framework, it's actually useful for some other senarios,
e.g. new release ASP.NET dynamic data applications(ASP.NET's answer to
scaffold)

My points is, a more flexible routing mechanism could make the
architect looks better and more flexible and you won't need to give up
the compatibility and CoC principle.

ASP.NET MVC's design learned a lot from Ruby On Rails, actually RoR's
route design is also quite flexible (refere to http://manuals.rubyonrails.com/read/chapter/65)

ygao

unread,
Jun 3, 2008, 8:56:47 PM6/3/08
to web...@googlegroups.com
On Wed, Jun 4, 2008 at 7:32 AM, maverick <zhiho...@gmail.com> wrote:

I am learning web2py and very new to Python, so I could be wrong or I
am not yet understand Massimo Di Pierro's principles, just some
opinions for discuss.

Looks like the URL rules  /[applicaiton]/[controller]/[function]  is
hard coded, I saw the code in main.py (lines near 140) :

           request.application=items[0]
           request.controller=items[1]
           request.function=items[2]

And also I found /static/... is hard coded as well.
after seen posted links,this is very ugly.

We can then refer to this "Products-Browse" rule explicitly within our Controllers and Views when we want to generate a URL to it.  For example, we could use the Html.RouteLink view helper to indicate that we want to link to our "Products-Browse" route and pass it a "Food" category parameter using code in our view template like below:

This view helper would then access the routing system and output an appropriate HTML hyperlink URL like below (note: how it did automatic parameter substitution of the category parameter into the URL using the route rule):

this is powerful tech.

Massimo Di Pierro

unread,
Jun 3, 2008, 10:06:44 PM6/3/08
to web...@googlegroups.com
The web2py way to use this is

{{=URL('application','controller','function')}}

I am not convinced the notation below buys anything other one additional layer of complexity in the syntax.

I believe the app should always refer to an "action" (i.e. controller functions) in a unique way that does not depend on how the administrator chooses to expose that action. I also believe that it is best to leave to the app to decide that URL('application','controller','function') maps into /applications/controller/function.

routes.py provides a mechanism to override this default mapping if one really needs to. Being able to change this mapping inside an app is looking for trouble because the app will then depend on how/where it is deployed and minimal changes can result in broken internal links.

Massimo

mdipierro

unread,
Jun 3, 2008, 10:13:35 PM6/3/08
to web2py Web Framework
and you can also do

{{=A('link text',_href=URL('application','controller','function')))}}

which results in

<a href="/application/controller/function">link text</a>

Mind that the web2py also escapes "link text" while I am not sure
the .NET way does.

Massimo

On Jun 3, 9:06 pm, Massimo Di Pierro <mdipie...@cs.depaul.edu> wrote:
> The web2py way to use this is
>
> {{=URL('application','controller','function')}}
>
> I am not convinced the notation below buys anything other one
> additional layer of complexity in the syntax.
>
> I believe the app should always refer to an "action" (i.e. controller
> functions) in a unique way that does not depend on how the
> administrator chooses to expose that action. I also believe that it
> is best to leave to the app to decide that URL
> ('application','controller','function') maps into /applications/
> controller/function.
>
> routes.py provides a mechanism to override this default mapping if
> one really needs to. Being able to change this mapping inside an app
> is looking for trouble because the app will then depend on how/where
> it is deployed and minimal changes can result in broken internal links.
>
> Massimo
>
> On Jun 3, 2008, at 7:56 PM, ygao wrote:
>
>
>
> > On Wed, Jun 4, 2008 at 7:32 AM, maverick <zhihong....@gmail.com>
> > wrote:
>
> > I am learning web2py and very new to Python, so I could be wrong or I
> > am not yet understand Massimo Di Pierro's principles, just some
> > opinions for discuss.
>
> > Looks like the URL rules /[applicaiton]/[controller]/[function] is
> > hard coded, I saw the code in main.py (lines near 140) :
>
> > request.application=items[0]
> > request.controller=items[1]
> > request.function=items[2]
>
> > And also I found /static/... is hard coded as well.
> > after seen posted links,this is very ugly.
> > We can then refer to this "Products-Browse" rule explicitly within
> > our Controllers and Views when we want to generate a URL to it.
> > For example, we could use the Html.RouteLink view helper to
> > indicate that we want to link to our "Products-Browse" route and
> > pass it a "Food" category parameter using code in our view template
> > like below:
>
> > This view helper would then access the routing system and output an
> > appropriate HTML hyperlink URL like below (note: how it did
> > automatic parameter substitution of the category parameter into the
> > URL using the route rule):
>

ygao

unread,
Jun 3, 2008, 10:25:08 PM6/3/08
to web...@googlegroups.com
On Wed, Jun 4, 2008 at 10:13 AM, mdipierro <mdip...@cs.depaul.edu> wrote:

and you can also do

{{=A('link text',_href=URL('application','controller','function')))}}

which results in

<a href="/application/controller/function">link text</a>

Mind that the web2py also escapes "link text" while I am not sure
the .NET way does.
I think the .NET way does is that it has a uniqe name to represent the url,it is better than railys way.
so  no matter  what your controller and function  is, you can change these as you will,even you refer the url in other app like web2py.
you only care about  friendly uniqe name and update the definition,that's all.

Massimo Di Pierro

unread,
Jun 3, 2008, 10:43:38 PM6/3/08
to web...@googlegroups.com
what about you define something like this in a model:

     class __LINK__:
          def __getattr__(self,value): return self[value]
          def __getitem__(self,value):
               c,f=value.split('_',1)
               return lambda args=[],vars={}: URL(r=request,c=c,f=f,args=args,vars=vars)
     link=__LINK__()

and then you use it like

    {{=link.controller_action()}} or {{=link['controller_action']()}}

would generate

    "/application/controller/action"

?

Massimo Di Pierro

unread,
Jun 3, 2008, 10:49:43 PM6/3/08
to web...@googlegroups.com
Or better below

what about you define something like this in a model:

     class __LINK__:
          def __getattr__(self,value): return self[value]
          def __getitem__(self,value):
               c,f=value.split('_',1)
               return lambda *args,**vars: URL(r=request,c=c,f=f,args=args,vars=vars)
     link=__LINK__()

and then you use it like

    {{=link.controller_action('bla','bla',name='value')}} 

would generate

    "/application/controller/action/bla/bla?name=value"

ygao

unread,
Jun 3, 2008, 10:52:00 PM6/3/08
to web...@googlegroups.com
On Wed, Jun 4, 2008 at 10:43 AM, Massimo Di Pierro <mdip...@cs.depaul.edu> wrote:
what about you define something like this in a model:

     class __LINK__:
          def __getattr__(self,value): return self[value]
          def __getitem__(self,value):
               c,f=value.split('_',1)
               return lambda args=[],vars={}: URL(r=request,c=c,f=f,args=args,vars=vars)
     link=__LINK__()

and then you use it like

    {{=link.controller_action()}} or {{=link['controller_action']()}}

would generate

    "/application/controller/action"

?
 
I think the .NET way  is global.not a single app or a group url.



--
※※※※※※※※※※※※※※※
http://BLOG.donews.com/ygao
UliPad:The powerful Python editor

Massimo Di Pierro

unread,
Jun 3, 2008, 11:06:33 PM6/3/08
to web...@googlegroups.com
I need to read it again when I wake up tomorrow. ;-)

Massimo

maverick

unread,
Jun 4, 2008, 3:47:56 AM6/4/08
to web2py Web Framework
In ASP.NET MVC, the route rule is applied for both URL parsing and URL
generating. That's to say, once u change the rule the links you
generated inside web pages become the same format. That's one good
design.

I don't think hard code all URL to /app/controller/function is a one
fit all solution, though it can handle 90% requirements. I just feel
this part is able to be improved without pain, keep back compatiable
and easy to use.
> > UliPad:The powerful Python editor- Hide quoted text -
>
> - Show quoted text -

mdipierro

unread,
Jun 4, 2008, 9:09:54 AM6/4/08
to web2py Web Framework
I will need to think about this. Perhaps this can be handle via a wsgi
plugin?

voltron

unread,
Jun 4, 2008, 10:08:39 AM6/4/08
to web2py Web Framework
Maybe a web2py WSGI plugin api would be cool, then well put up a
repository just like for validators

maverick

unread,
Jun 4, 2008, 10:51:29 AM6/4/08
to web2py Web Framework
handle via a wsgi plugin is just beautiful design !

I think that why ASP.NET don't put route inside ASP.NET MVC framework,
and keep it a stand along component.

Massimo Di Pierro

unread,
Jun 4, 2008, 11:13:24 AM6/4/08
to web...@googlegroups.com
at what level would you add a wsgi plugin? Before web2py is called?

Massimo

voltron

unread,
Jun 4, 2008, 1:21:47 PM6/4/08
to web2py Web Framework
it depends, most of the plugins would be code that would modify the
response or request object in some way. Is it possible to get web2py
to "poll" a plugin folder? Then the point at which the plugin is
called is irrelevant

voltron

unread,
Jun 4, 2008, 1:23:14 PM6/4/08
to web2py Web Framework
Could you explain a little more Maverick? You do or do not like WSGI
plugins?

maverick

unread,
Jun 4, 2008, 5:26:14 PM6/4/08
to web2py Web Framework
I DO like the plugin idea.

In Java, it's called servlet filter, it's pretty useful. it can be add
like a chain, it will intercept the request and response.

In ASP.Net, i think it's the httpModule. In ASP.NET MVC, I believe
the route is actually a httpModule, it then activate the action
classes, and use tranditional ASP.NET to render the view (you can
change any other render engine)

I am still very new to Python, so I don't have an idea how and where
to put, i will learn more and dicuss here.
> > > > > > - Show quoted text -- Hide quoted text -

maverick

unread,
Jun 4, 2008, 5:32:12 PM6/4/08
to web2py Web Framework
I think wrap the request/response is a good idea. to make it simpler,
we can predefine like "BeforeRequest", "AfterRequest", or even some
more high level "BeforeAuthentication"... just very raw idea...
> > >>>> - Show quoted text -- Hide quoted text -

Massimo Di Pierro

unread,
Jun 4, 2008, 6:37:08 PM6/4/08
to web...@googlegroups.com
This we can do already because web2py is build on the wsgi standard.
It only needs to be documented. One problem is that any wsgi plugin
either applies to all controllers or none. web2py is wsgi, individual
web2py apps are not.

Massimo

Stefano

unread,
Jun 5, 2008, 4:43:19 AM6/5/08
to web...@googlegroups.com
This is what I had in mind when I wrote about "Middleware" in my other mail. The concept of "filter" is better understandable. You proposed to add decorators to methods who needed this pre/post processing, but I think it would be more useful is it was possible to define a "general" chain of processing.


2008/6/5 Massimo Di Pierro <mdip...@cs.depaul.edu>:



--
~
~
:wq

Massimo Di Pierro

unread,
Jun 5, 2008, 10:16:27 AM6/5/08
to web...@googlegroups.com
so something outside web2py?

Stefano

unread,
Jun 5, 2008, 11:23:19 AM6/5/08
to web...@googlegroups.com
With apache2 filters, you mean? It's possible, but not very convenient.
And it could even not be an option if you need to mangle with data in the session, wich could be also used elsewhere in your web2py application.

Don't know how much important this feature would be anyway.
  S
--
~
~
:wq
Reply all
Reply to author
Forward
0 new messages