On Wednesday, August 3, 2016 at 2:00:08 AM UTC-4, Carlos Cesar Caballero
wrote:
>
> Hi, first sorry for the late reply, but I am on vacations and offline most
> of the time.
>
> Here is an example on how we use the Yii2 framework router:
>
> 'urlManager' => [
> 'class' => \frontend\components\UrlManager::className(),
> 'enablePrettyUrl' => true,
> 'showScriptName' => false,
> // 'suffix' => '.daxs',
> 'rules' => [
> //contact
> ['pattern' => 'contact', 'route' => '/site/contact/',
> 'defaults' => ['lang' => 'en-US']],
> ['pattern' => 'contactarme', 'route' => '/site/contact/',
> 'defaults' => ['lang' => 'es']],
> //about
> ['pattern' => 'en/about', 'route' => '/blog/post/view/',
> 'defaults' => ['lang' => 'en-US', 'postSlug' => 'about']],
> ['pattern' => 'es/acerca-de', 'route' =>
> '/blog/post/view/', 'defaults' => ['lang' => 'es', 'postSlug' =>
> 'acerca-de']],
> //curriculum
> ['pattern' => 'en/curriculum', 'route' =>
> '/blog/post/view/', 'defaults' => ['lang' => 'en-US', 'postSlug' =>
> 'curriculum-2']],
> ['pattern' => 'es/curriculum', 'route' =>
> '/blog/post/view/', 'defaults' => ['lang' => 'es', 'postSlug' =>
> 'curriculum']],
> //feed
> ['pattern' => 'en/feed', 'route' => '/blog/post/feed',
> 'defaults' => ['lang' => 'en-US'], 'suffix' => ".xml"],
> ['pattern' => 'es/canal', 'route' => '/blog/post/feed',
> 'defaults' => ['lang' => 'es'], 'suffix' => ".xml"],
> //categories
> ['pattern' => 'en/<categorySlug>', 'route' =>
> '/blog/post/category', 'defaults' => ['lang' => 'en-US']],
> ['pattern' => 'es/<categorySlug>', 'route' =>
> '/blog/post/category', 'defaults' => ['lang' => 'es']],
> //articles
> ['pattern' => 'en/<categorySlug>/<postSlug>', 'route' =>
> '/blog/post/view', 'defaults' => ['lang' => 'en-US']],
> ['pattern' => 'es/<categorySlug>/<postSlug>', 'route' =>
> '/blog/post/view', 'defaults' => ['lang' => 'es']],
> ]
> ]
>
> As you can see, there is no need of routes in and routes out, it
> automatically build in and out based on the rules,
>
Note, in web2py, you can write code to automatically build routes_out based
on routes_in (this is particularly simple when using the $placeholder
syntax rather than regular expressions). I think Yii is able to do this
automatically because it does not allow arbitrary regular expressions
anywhere in the URL, and where it does allow regular expressions, they are
paired with parameter names to identify them.
In that way we can move the code to a new application, and use a complete
> different urls without change one single line of code.
>
I think the only thing web2py can't easily do is map named parameters to
particular path info positions when generating *outgoing *URLs (it can
handle this for incoming URLs). You can use URL args to specify elements of
the path, but the app code must specify the *order *of the items, which
means the order cannot be changed without changing the app code. However,
mapping named parameters to specific positions in the path can be kluged as
follows:
routes_in = (
('/myapp/default/index/(?P<varB>[\w-]+)/(?P<varA>[\w-]+)',
'/myapp/default/index?varA=\g<varA>&varB=\g<varB>'),
)
routes_out = (
('/myapp/default/index/varA_(?P<varA>[\w-]+)/varB_(?P<varB>[\w-]+)',
'/myapp/default/index/\g<varB>/\g<varA>'),
)
You would then create a URL like this:
URL('default', 'index', args=['varA_Value-A', 'varB_Value-B'])
and you will get:
/myapp/default/index/Value-B/Value-A
which when clicked will take you to the index function in the default
controller, with request.get_vars = {'varA': 'Value-A', 'varB': 'Value-B'}.
Now you can change the order of varA and varB in the incoming/outgoing URL
path by editing the routes only without touching the app code.
Anyway, aside from the issue of keeping the order of the elements of the
URL path independent of the app code, I think the web2py rewrite system can
accommodate your requirements. And of course, you could always write a
custom URL helper to generate the outgoing URLs however you like -- this
would obviously take a bit more work, but it would be a one-time effort.
Anthony
--
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
---
You received this message because you are subscribed to the Google Groups
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.