mirror of
https://github.com/kataras/iris.git
synced 2025-02-09 02:34:55 +01:00
add routing -> dependency injection section
parent
7f414b4276
commit
d88a3146f4
3
Home.md
3
Home.md
|
@ -14,7 +14,8 @@ This wiki is the main source of documentation for **developers** working with (o
|
||||||
* [[Middleware|Routing-middleware]]
|
* [[Middleware|Routing-middleware]]
|
||||||
* [[Handle HTTP errors|Routing-error-handlers]]
|
* [[Handle HTTP errors|Routing-error-handlers]]
|
||||||
* [[Wrap the Router|Routing-wrap-the-router]]
|
* [[Wrap the Router|Routing-wrap-the-router]]
|
||||||
|
* [[Dependency Injection|Routing-dependency-injection]]
|
||||||
|
|
||||||
## Runnable Examples
|
## Runnable Examples
|
||||||
|
|
||||||
We've crafted more than 100 ready to run examples for your learning curve. Navigate through <https://github.com/kataras/iris/tree/master/_examples>.
|
We've crafted more than 100 ready to run examples for your learning curve. [Browse through them](https://github.com/kataras/iris/tree/master/_examples).
|
45
Routing-dependency-injection.md
Normal file
45
Routing-dependency-injection.md
Normal file
|
@ -0,0 +1,45 @@
|
||||||
|
The subpackage [hero](https://github.com/kataras/iris/tree/master/hero) contains features for binding any object or function that handlers can accept on their input arguments, these are called dependencies.
|
||||||
|
|
||||||
|
A dependency can be either `Static` for things like Services or `Dynamic` for values that depend on the incoming request.
|
||||||
|
|
||||||
|
With Iris you get truly safe bindings. It is blazing-fast, near to raw handlers performance because Iris tries to calculates everything before the server even goes online!
|
||||||
|
|
||||||
|
Iris provides built-in dependencies to match route's parameters with func input arguments that you can use right away.
|
||||||
|
|
||||||
|
To use this feature you should import the hero subpackage:
|
||||||
|
|
||||||
|
```go
|
||||||
|
import (
|
||||||
|
// [...]
|
||||||
|
"github.com/kataras/iris/hero"
|
||||||
|
)
|
||||||
|
```
|
||||||
|
|
||||||
|
And use its `hero.Handler` package-level function to build a handler from a function that can accept dependencies, like this:
|
||||||
|
|
||||||
|
```go
|
||||||
|
func printFromTo(from, to string) { /* [...]*/ }
|
||||||
|
|
||||||
|
// [...]
|
||||||
|
app.Get("/{from}/{to}", hero.Handler(printFromTo))
|
||||||
|
```
|
||||||
|
|
||||||
|
As you've seen above the `iris.Context` input argument is totally optional. Of course you can still declare it as **first input argument** - Iris is smart enough to bind it as well without any hassle.
|
||||||
|
|
||||||
|
Below you will see some screenshots designed to facilitate your understanding:
|
||||||
|
|
||||||
|
## 1. Path Parameters - Built-in Dependencies
|
||||||
|
|
||||||
|
[[_assets/hero/hero-1-monokai.png]]
|
||||||
|
|
||||||
|
## 2. Services - Static Dependencies
|
||||||
|
|
||||||
|
[[_assets/hero/hero-2-monokai.png]]
|
||||||
|
|
||||||
|
## 3. Per-Request - Dynamic Dependencies
|
||||||
|
|
||||||
|
[[_assets/hero/hero-3-monokai.png]]
|
||||||
|
|
||||||
|
Honestly, `hero funcs` are very easy to understand and when you start using them **you never go back**.
|
||||||
|
|
||||||
|
Later on you'll see how this knowledge will help you to craft an application using the MVC architectural pattern which Iris provides wonderful API for it.
|
|
@ -9,3 +9,4 @@
|
||||||
* [[Middleware|Routing-middleware]]
|
* [[Middleware|Routing-middleware]]
|
||||||
* [[Custom error handlers|Routing-error-handlers]]
|
* [[Custom error handlers|Routing-error-handlers]]
|
||||||
* [[Wrap the Router|Routing-wrap-the-router]]
|
* [[Wrap the Router|Routing-wrap-the-router]]
|
||||||
|
* [[Dependency Injection|Routing-dependency-injection]]
|
BIN
_assets/hero-1-monokai.png
Normal file
BIN
_assets/hero-1-monokai.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 73 KiB |
BIN
_assets/hero-2-monokai.png
Normal file
BIN
_assets/hero-2-monokai.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 208 KiB |
BIN
_assets/hero-3-monokai.png
Normal file
BIN
_assets/hero-3-monokai.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 257 KiB |
Loading…
Reference in New Issue
Block a user