Skip to Main Content
Developing Web Applications with Haskell and Yesod
book

Developing Web Applications with Haskell and Yesod

by Michael Snoyman
April 2012
Intermediate to advanced content levelIntermediate to advanced
294 pages
6h 27m
English
O'Reilly Media, Inc.
Content preview from Developing Web Applications with Haskell and Yesod

Chapter 7. Routing and Handlers

If we look at Yesod as a Model-View-Controller framework, routing and handlers make up the controller. For contrast, let’s describe two other routing approaches used in other web development environments:

  • Dispatch based on file name. This is how PHP and ASP work, for example.

  • Have a centralized routing function that parses routes based on regular expressions. Django and Rails follow this approach.

Yesod is closer in principle to the latter technique. Even so, there are significant differences. Instead of using regular expressions, Yesod matches on pieces of a route. Instead of having a one-way route-to-handler mapping, Yesod has an intermediate data type (called the route data type, or a type-safe URL) and creates two-way conversion functions.

Coding this more advanced system manually is tedious and error prone. Therefore, Yesod defines a Domain Specific Language (DSL) for specifying routes, and provides Template Haskell functions to convert this DSL to Haskell code. This chapter will explain the syntax of the routing declarations, give you a glimpse of what code is generated for you, and explain the interaction between routing and handler functions.

Route Syntax

Instead of trying to shoe-horn route declarations into an existing syntax, Yesod’s approach is to use a simplified syntax designed just for routes. This has the advantage of making the code not only easy to write, but simple enough for someone with no Yesod experience to read and understand the ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Developing Web Apps with Haskell and Yesod, 2nd Edition

Developing Web Apps with Haskell and Yesod, 2nd Edition

Michael Michael Snoyman Snoyman

Publisher Resources

ISBN: 9781449336868Supplemental ContentErrata Page