diff --git a/Home.md b/Home.md index 237c083..6ab115b 100644 --- a/Home.md +++ b/Home.md @@ -14,7 +14,8 @@ This wiki is the main source of documentation for **developers** working with (o * [[Middleware|Routing-middleware]] * [[Handle HTTP errors|Routing-error-handlers]] * [[Wrap the Router|Routing-wrap-the-router]] + * [[Dependency Injection|Routing-dependency-injection]] ## Runnable Examples -We've crafted more than 100 ready to run examples for your learning curve. Navigate through . \ No newline at end of file +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). \ No newline at end of file diff --git a/Routing-dependency-injection.md b/Routing-dependency-injection.md new file mode 100644 index 0000000..dba6fc0 --- /dev/null +++ b/Routing-dependency-injection.md @@ -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. diff --git a/_Sidebar.md b/_Sidebar.md index 2ce59bc..155f236 100644 --- a/_Sidebar.md +++ b/_Sidebar.md @@ -9,3 +9,4 @@ * [[Middleware|Routing-middleware]] * [[Custom error handlers|Routing-error-handlers]] * [[Wrap the Router|Routing-wrap-the-router]] + * [[Dependency Injection|Routing-dependency-injection]] \ No newline at end of file diff --git a/_assets/hero-1-monokai.png b/_assets/hero-1-monokai.png new file mode 100644 index 0000000..fcc6696 Binary files /dev/null and b/_assets/hero-1-monokai.png differ diff --git a/_assets/hero-2-monokai.png b/_assets/hero-2-monokai.png new file mode 100644 index 0000000..975ffe1 Binary files /dev/null and b/_assets/hero-2-monokai.png differ diff --git a/_assets/hero-3-monokai.png b/_assets/hero-3-monokai.png new file mode 100644 index 0000000..178e6ee Binary files /dev/null and b/_assets/hero-3-monokai.png differ