The Pyramid Router ● Created when you start a Pyramid app. ● From the glossary: The router intercepts requests, invokes traversal and/or URL dispatch, calls view functions, and returns responses to the WSGI server on behalf of your Pyramid application.
PyramidPress Example ● Home page - Show a list of our users. ● User view - web visitor looks at a member's blog. e.g. for Bob, his page is: /bob ● Post view - web visitor looks at a member's single post. e.g. for Bob's motorbike page: /bob/motorbikes
Traversal Algorithm ● Resources of two types: ○ Container - dictionary-like, has __getitem__. ○ Leaf. ● Walk the URL until: ○ Path is exhausted. ○ KeyError is raised. ○ Non-final element has no __getitem__. ○ Path element starts with @@.
Traversal Example ● With '/' ● Pyramid Router makes [] ● Returns root_resource. ● Path exhausted. ● Returns Root in response['context'] ● View lookup now finds suitable view for Root context and calls it.
Traversal Example ● With '/bob/motors' ● Pyramid Router makes [u'bob', u'motors'] ● Calls root_resource.__getitem__(u'bob') ○ Which returns ● Calls bob.__getitem__(u'motors') ○ Which returns ● Path exhausted. ● Returns Motors page in response['context'] ● View lookup again goes to work.
Hybrid Applications ● Play with Traversal in a safe way. ● Routes like: config.add_route('user', '/user/*traverse') config.add_route('user_page', '/user/{page}/*traverse') ● More URL Dispatch than Traversal.
Thanks for listening All code is on GitHub: github.com/jamescooke/pyramid-london-talk Find me on Twitter: @jamesfublo Ask me questions? I'll do my best to answer :D