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

The Handler Monad

The vast majority of code you write in Yesod sits in the Handler monad. If you are approaching this from an MVC (Model-View-Controller) background, your Handler code is the Controller. Some important points to know about Handler:

  • It is an instance of MonadIO, so you can run any IO action in your handlers with liftIO. By the way, liftIO is exported by the Yesod module for your convenience.

  • Like Widget, Handler is a fake-monad-transformer. It wraps around a ResourceT IO monad. We discuss this type at length in the conduits appendix, but for now, we’ll just say it let’s you safely allocate resources.

  • By “fake,” I mean you can’t use the standard lift function provided by the transformers package, you must use the Yesod-supplied one (just like with widgets).

  • Handler is just a type synonym around GHandler. GHandler let’s you specify exactly which subsite and master site you’re using. The Handler synonym says that the sub and master sites are your application’s type.

  • Handler provides a lot of different functionality, such as:

    • Providing request information.

    • Keeping a list of the extra response headers you’ve added.

    • Allowing you to modify the user’s session.

    • Short-circuiting responses, for redirecting, sending static files, or reporting errors.

The remainder of this chapter will give a brief introduction to some of the most common functions living in the Handler monad. I am specifically not covering any of the session functions; that will be addressed in the sessions chapter.

Application ...

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