How to use "get" parameters or parameters in the URL?
All pages are services (How does a page's source code look?). These services has two parameters: path and get_params.
- The first parameter, path, is used to set choose the path URL.
- The second parameter, get_params, is the one that interests you!
path
This argument is a list of string, corresponding to the URL where your page service can be found.
For example, if you want your page to be accessed via this URL:
http://website.com/a/b/c
You must provide this path:
~path:["a";"b";"c"]
This parameter can also be an empty list. It means that the service page will be accessed via this URL:
http://website.com/get_params
Services can have several parameters of several types.
Don't forget to open the module Eliom_parameter at the beginning of your file!
Syntax for several parameters
- No parameter
~get_params:unit
- One parameter
~get_params:(int "id")
- More parameters
~get_params:(string "name" ** int "age" ** bool "graduate")
Types of parameters
- Common types: string, int, int32, float, bool
- Some other types: optional values, any parameter, list, your own type.
- See the whole list of available parameters in Eliom documentation
Get these parameters
On service registration, parameters can be retrieved as a parameter of the function:
Example.register ~service:the_service (fun parameters () -> ...)
- No parameter
(fun () () -> ...)
- One parameter
(fun a () -> ...)
- Several parameters
The operator is left associative:
(fun (a, (b, (c, d)))) () -> ...)
Variable paths
Parameters provided in the path are constant. It's possible to have a variable URL!
Imagine you want to offer a personal page to your users. You will need a variable URL that will look like this, where xxx can be replace by the username:
http://website.com/users/xxxTo do that, add a parameter of type suffix in get_params:
~get_params:(suffix (string "username"))