The vast majority of code you write in Yesod sits in the
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
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
for your convenience.
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
By “fake,” I mean you can’t use the standard
function provided by the
transformers package, you
must use the Yesod-supplied one (just like with widgets).
Handler is just a type synonym around
you specify exactly which subsite and master site you’re using. The
Handler synonym says that the sub and master sites are your
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.