web2py does not use decorators to specify routes like Flask does (which can 
get cumbersome and difficult to maintain with lots of routes). Instead, 
first, you get default routing without making any explicit routing 
specifications -- see the link I posted earlier: 
http://web2py.com/books/default/chapter/29/4#Dispatching. In web2py, your 
code translates to:

In /myapp/controllers/default.py:

def index():
    return 'Welcome'

def articles():
    if request.args:
        return 'Your are reading ' + request.args(0)
    else:
        return 'List of ' + URL()

That will give you URLs like the following:

/myapp
/myapp/default/articles
/myapp/default/articles/1

Now, to get rid of "myapp" and "default", you can use one of the two web2py URL 
rewrite systems <http://web2py.com/books/default/chapter/29/4#URL-rewrite>. 
In this case, the simplest is the parameter-based 
system<http://web2py.com/books/default/chapter/29/4#Parameter-based-system>. 
Just create a routes.py file in the /web2py folder with the following 
content:

routers = dict(
    BASE = dict(
        default_application = 'myapp'
    )
)

Specifying the default application, controller, and function removes them 
from the URLs. The default controller and default function are already set 
to "default" and "index", respectively, so you don't have to specify those 
explicitly in the router (you would have to specify them if you used 
different names). So, by creating the routes.py file and adding "myapp" as 
the default application, we now get the routes you want:

/
/articles
/articles/1

You might also want to look at 
http://web2py.com/books/default/chapter/29/10#Restful-Web-Services.

Anthony

On Thursday, August 16, 2012 5:05:21 AM UTC-4, deepak wrote:
>
> I would like to perform REST Services by mapping url to the 
> controller-function. For eg:
>
> @app.route('/')def api_root():
>     return 'Welcome'
> @app.route('/articles')def api_articles():
>     return 'List of ' + url_for('api_articles')
> @app.route('/articles/<articleid>')def api_article(articleid):
>     return 'You are reading ' + articleid
>
> which could be done in Flask. Basically i would want to match the URLs to 
> the service in web2py.
>
> On Tuesday, 14 August 2012 23:43:20 UTC+5:30, Anthony wrote:
>>
>> It looks like you could do all of that with web2py. Can you be more 
>> specific regarding how you want your URLs to look and what they should 
>> retrieve. In addition to RESTful services and routing, have you looked at 
>> the basics: http://web2py.com/books/default/chapter/29/4#Dispatching?
>>
>> Anthony
>>
>> On Tuesday, August 14, 2012 1:10:58 PM UTC-4, deepak wrote:
>>>
>>> I want to use RESTful URL patters likewise in Flask, '
>>> http://publish.luisrei.com/articles/flaskrest.html'
>>>>
>>>> All I could end up finding was to make use of routes.py [routes_in & 
>>> routes_out]. 
>>>
>>> Deepak
>>>
>>

-- 



Reply via email to