Routing Basics
In your project, routes are defined in theWeb/Routes.hs. In addition to defining that route, it also has to be added in Web/FrontController.hs to be picked up by the routing system.
The simplest way to define a route is by using AutoRoute, which automatically maps each controller action to an URL. For a PostsController, the definition in Web/Routes.hs will look like this:
PostsController in Web/FrontController.hs like this:
/Posts to access the PostsAction.
Changing the Start Page / Home Page
You can define a custom start page action using thestartPage function like this:
startPage WelcomeAction defined. Make sure to remove this line. Otherwise, you will still see the default IHP welcome page.
The
WelcomeAction controller is provided by the separate ihp-welcome package, which is typically only used in new projects for the initial boilerplate.URL Generation
UsepathTo to generate a path to a given action:
urlTo:
AutoRoute
Let’s say ourPostsController is defined in Web/Types.hs like this:
instance AutoRoute PostsController will give us the following routing:
AutoRoute & Beautiful URLs
Lots of modern browsers don’t even show the full URL bar anymore (e.g. Safari and most mobile browsers). Therefore AutoRoute doesn’t aim to generate the “most” beautiful URLs out of the box. It’s rather optimized for the needs of developers. If you need beautiful URLs for SEO reasons, instead of using AutoRoute you can use the more manual APIs of IHP Routing.Multiple Parameters
An action constructor can have multiple parameters:Parameter Types
AutoRoute works with the following parameter types:Text[Text]Maybe TextInt[Int]Maybe IntId(for all model types)
Nothing, the value will be left out of the query parameter. Otherwise it will be included with the value:
Request Methods
When an action is named a certain way, AutoRoute will pick a certain request method for the route. E.g. for aDeletePostAction it will only allow requests with the request method DELETE because the action name starts with Delete. Here is an overview of all naming patterns and their corresponding request method:
Application Prefix
When using multiple applications in your IHP project, e.g. having an admin back-end, AutoRoute will prefix the action URLs with the application name. E.g. a controllerHelloWorldController defined in Admin/Types.hs will be automatically prefixed with /admin and generate URLs such as /admin/HelloAction.
This prefixing has special handling for the Web module so that all controllers in the default Web module don’t have a prefix.
Custom Routing
Sometimes you have special needs for your routing. For this case, IHP provides a lower-level routing API on whichAutoRoute is built.
Let’s say we have a controller like this:
/posts to map to ShowAllMyPostsAction. For that we need to add a CanRoute instance:
parseRoute' function is a parser that reads an URL and returns an action of type PostsController. The router uses attoparsec.
Next to the routing itself, we also need to implement the URL generation:
Beautiful URLs
Let’s say we want to give our blog post application a beautiful URL structure for SEO reasons. Our controller is defined as:posts table to have a field slug :: Text.
Now we define our CanRoute instance like this:
HasPath instance:
Helper Functions
TheIHP.RouterSupport module includes helpers functions such as:
parseUUIDto parse and return an UUIDparseIdto parse an UUID, afterwards wraps it in an IdparseTextto parse until the next/characterrouteParamto parse route query parameters
Method Override Middleware
HTML forms don’t support special HTTP methods likeDELETE. To work around this issue, IHP has a middleware which transforms e.g. a POST request with a form field _method set to DELETE to a DELETE request.
Custom 403 and 404 pages
You can override the default 403 access denied and the default 404 not found pages by creating a new file atstatic/403.html and static/404.html. Then IHP will render that HTML file instead of displaying the default IHP page.