|
| 1 | +--- |
| 2 | +layout: quickstart |
| 3 | +title: Quickstart |
| 4 | +--- |
| 5 | +Revel tries to make the conversion of parameters into their desired Go types as |
| 6 | +easy as possible. This conversion from string to another type is referred to as |
| 7 | +"data binding". |
| 8 | + |
| 9 | +## Params |
| 10 | + |
| 11 | +All request parameters are collected into a single `Params` object. That includes: |
| 12 | + |
| 13 | +* URL Path parameters |
| 14 | +* URL Query parameters |
| 15 | +* Form values (Multipart or not) |
| 16 | +* File uploads |
| 17 | + |
| 18 | +This is the definition ([godoc](../docs/godoc/params.html)): |
| 19 | + |
| 20 | +<pre class="prettyprint lang-go"> |
| 21 | +type Params struct { |
| 22 | + url.Values |
| 23 | + Files map[string][]*multipart.FileHeader |
| 24 | +} |
| 25 | +</pre> |
| 26 | + |
| 27 | +The embedded `url.Values` ([godoc](http://www.golang.org/pkg/net/url/#Values)) |
| 28 | +does provide accessors for simple values, but developers will find it easier to |
| 29 | +use Revel's data-binding mechanisms for any non-string values. |
| 30 | + |
| 31 | +## Action arguments |
| 32 | + |
| 33 | +Parameters may be accepted directly as method arguments by the action. For |
| 34 | +example: |
| 35 | + |
| 36 | +<pre class="prettyprint lang-go"> |
| 37 | +func (c AppController) Action(name string, ids []int, user User, img []byte) revel.Result { |
| 38 | + ... |
| 39 | +} |
| 40 | +</pre> |
| 41 | + |
| 42 | +Before invoking the action, Revel asks its Binder to convert parameters of those |
| 43 | +names to the requested data type. If the binding is unsuccessful for any |
| 44 | +reason, the parameter will have the zero value for its type. |
| 45 | + |
| 46 | +## Binder |
| 47 | + |
| 48 | +To bind a parameter to a data type, use Revel's Binder |
| 49 | +([godoc](../docs/godoc/binder.html)). It is integrated with the Params object |
| 50 | +as the following example shows: |
| 51 | + |
| 52 | +{% raw %} |
| 53 | +<pre class="prettyprint lang-go"> |
| 54 | +func (c SomeController) Action() revel.Result { |
| 55 | + var ids []int |
| 56 | + c.Params.Bind(&ids, "ids") |
| 57 | + ... |
| 58 | +} |
| 59 | +</pre> |
| 60 | +{% endraw %} |
| 61 | + |
| 62 | +The following data types are supported out of the box: |
| 63 | + |
| 64 | +* Ints of all widths |
| 65 | +* Bools |
| 66 | +* Pointers to any supported type |
| 67 | +* Slices of any supported type |
| 68 | +* Structs |
| 69 | +* time.Time for dates and times |
| 70 | +* \*os.File, \[\]byte, io.Reader, io.ReadSeeker for file uploads |
| 71 | + |
| 72 | +The following sections describe the syntax for these types. It is also useful |
| 73 | +to refer to [the source code](../docs/src/binder.html) if more detail is required. |
| 74 | + |
| 75 | +### Booleans |
| 76 | + |
| 77 | +The string values "true", "on", and "1" are all treated as **true**. Else, the |
| 78 | +bound value will be **false**. |
| 79 | + |
| 80 | +### Slices |
| 81 | + |
| 82 | +There are two supported syntaxes for binding slices: ordered or unordered. |
| 83 | + |
| 84 | +Ordered: |
| 85 | + |
| 86 | + ?ids[0]=1 |
| 87 | + &ids[1]=2 |
| 88 | + &ids[3]=4 |
| 89 | + |
| 90 | +Results in the slice `[]int{1, 2, 0, 4}` |
| 91 | + |
| 92 | +Unordered: |
| 93 | + |
| 94 | + ?ids[]=1 |
| 95 | + &ids[]=2 |
| 96 | + &ids[]=3 |
| 97 | + |
| 98 | +results in the slice `[]int{1, 2, 3}` |
| 99 | + |
| 100 | +**Note:** Only ordered slices should be used when binding a slice of structs: |
| 101 | + |
| 102 | + ?user[0].Id=1 |
| 103 | + &user[0].Name=rob |
| 104 | + &user[1].Id=2 |
| 105 | + &user[1].Name=jenny |
| 106 | + |
| 107 | +### Structs |
| 108 | + |
| 109 | +Structs are bound using a simple dot notation: |
| 110 | + |
| 111 | + ?user.Id=1 |
| 112 | + &user.Name=rob |
| 113 | + &user.Friends[]=2 |
| 114 | + &user.Friends[]=3 |
| 115 | + &user.Father.Id=5 |
| 116 | + &user.Father.Name=Hermes |
| 117 | + |
| 118 | +would bind a structure defined as: |
| 119 | + |
| 120 | +<pre class="prettyprint lang-go"> |
| 121 | +type User struct { |
| 122 | + Id int |
| 123 | + Name string |
| 124 | + Friends []int |
| 125 | + Father User |
| 126 | +} |
| 127 | +</pre> |
| 128 | + |
| 129 | +**Note:** Properties must be exported in order to be bound. |
| 130 | + |
| 131 | +### Date / Time |
| 132 | + |
| 133 | +The SQL standard time formats \["2006-01-02", "2006-01-02 15:04"\] are built in. |
| 134 | + |
| 135 | +More may be added by the application, using |
| 136 | +[the official pattern](http://golang.org/pkg/time/#pkg-constants). Simply add |
| 137 | +the pattern to recognize to the `TimeFormats` variable, like this: |
| 138 | + |
| 139 | +<pre class="prettyprint lang-go"> |
| 140 | +func init() { |
| 141 | + revel.TimeFormats = append(revel.TimeFormats, "01/02/2006") |
| 142 | +} |
| 143 | +</pre> |
| 144 | + |
| 145 | +### File Uploads |
| 146 | + |
| 147 | +File uploads may be bound to any of the following types: |
| 148 | + |
| 149 | +* \*os.File |
| 150 | +* \[\]byte |
| 151 | +* io.Reader |
| 152 | +* io.ReadSeeker |
| 153 | + |
| 154 | +This is a wrapper around the upload handling provided by |
| 155 | +[Go's multipart package](http://golang.org/pkg/mime/multipart/). The bytes |
| 156 | +stay in memory unless they exceed a threshold (10MB by default), in which case |
| 157 | +they are written to a temp file. |
| 158 | + |
| 159 | +**Note:** Binding a file upload to `os.File` requires Revel to write it to a |
| 160 | +temp file (if it wasn't already), making it less efficient than the other types. |
| 161 | + |
| 162 | +### Custom Binders |
| 163 | + |
| 164 | +The application may define its own binders to take advantage of this framework. |
| 165 | + |
| 166 | +It need only implement the [binder interface](../docs/godoc/binder.html#Binder) and register the type for which it |
| 167 | +should be called: |
| 168 | + |
| 169 | +<pre class="prettyprint lang-go"> |
| 170 | +var myBinder = revel.Binder{ |
| 171 | + Bind: func(params *revel.Params, name string, typ reflect.Type) reflect.Value {...}, |
| 172 | + Unbind: func(output map[string]string, name string, val interface{}) {...}, |
| 173 | +} |
| 174 | + |
| 175 | +func init() { |
| 176 | + revel.TypeBinders[reflect.TypeOf(MyType{})] = myBinder |
| 177 | +} |
| 178 | +</pre> |
0 commit comments