O'Reilly logo

Developing Web Applications with Haskell and Yesod by Michael Snoyman

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Chapter 6. Yesod Typeclass

Every one of our Yesod applications requires an instance of the Yesod typeclass. So far, we’ve only seen defaultLayout. In this chapter, we’ll explore the meaning of many of the methods of the Yesod typeclass.

The Yesod typeclass gives us a central place for defining settings for our application. Everything else has a default definition that is usually the right thing. But in order to build a powerful, customized application, you’ll usually end up wanting to override at least a few of these methods.

Rendering and Parsing URLs

We’ve already mentioned how Yesod is able to automatically render type-safe URLs into a textual URL that can be inserted into an HTML page. Let’s say we have a route definition that looks like:

mkYesod "MyApp" [parseRoutes|
/some/path SomePathR GET
]

If we place SomePathR into a hamlet template, how does Yesod render it? Yesod always tries to construct absolute URLs. This is especially useful once we start creating XML sitemaps and Atom feeds, or sending emails. But in order to construct an absolute URL, we need to know the domain name of the application.

You might think we could get that information from the user’s request, but we still need to deal with ports. And even if we get the port number from the request, are we using HTTP or HTTPS? And even if you know that, such an approach would mean that different URLs would be generated depending on how the user submitted a request. For example, we would generate different URLs depending on ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required