controller/

directory
v14.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 12, 2026 License: BSD-3-Clause

README

Controller

A controller groups related routes into one type: shared middleware in one place, request handling next to the state it needs. Implement the Routing(r iris.Router) method to build one, then register it with app.Router. Example of a controller:

package main

import (
    "github.com/kataras/iris/v14"
    "github.com/kataras/iris/v14/controller/fileserver"
)

func main() {
    app := iris.New()

    // Register the built-in fileserver controller.
	app.Router("/static", fileserver.New("./assets", fileserver.Options{
		IndexName: "index.html",
		ShowList:  true,
	}))

    // Register a custom controller.
    app.Router("/users", new(UserController))

    app.Listen(":8080")
}

type UserController struct{}

func (c *UserController) Routing(r iris.Router) {
    r.Get("/", c.List)
    r.Post("/", c.Create)
    r.Get("/{id:int}", c.Get)
    r.Put("/{id:int}", c.Update)
    r.Delete("/{id:int}", c.Delete)
}

func (c *UserController) List(ctx iris.Context) {
    ctx.WriteString("List users")
}

func (c *UserController) Create(ctx iris.Context) {
    ctx.WriteString("Create user")
}

func (c *UserController) Get(ctx iris.Context) {
    id := ctx.Params().GetInt("id")
    ctx.Writef("Get user %d", id)
}

func (c *UserController) Update(ctx iris.Context) {
    id := ctx.Params().GetInt("id")
    ctx.Writef("Update user %d", id)
}

func (c *UserController) Delete(ctx iris.Context) {
    id := ctx.Params().GetInt("id")
    ctx.Writef("Delete user %d", id)
}

Built-in Controllers

Controller Description Documentation
API Graph The API Graph router Controller registers a handler that renders a graph of the application's routes. It generates an HTML page with a D3.js visualization of the routes, showing the paths, methods, and handlers. The graph is interactive: click a handler to open its file in Visual Studio Code, hover over a link or a node for more details. apigraph/README
File Server The File Server router controller and handler serves static files from a specified directory. It is useful for delivering assets like images, CSS, JavaScript files, and downloadable content. fileserver/README
MVC The MVC router controller supports the Model-View-Controller design pattern: the application is split into separate layers, which are easier to maintain and test. mvc/README
Sitemap The Sitemap router Controller generates XML sitemaps for your application automatically. This helps search engines crawl and index your website more effectively. sitemap/README

Directories

Path Synopsis
Package fileserver serves static files from a directory, an embed.FS or any fs.FS, through the New controller or the Handler function.
Package fileserver serves static files from a directory, an embed.FS or any fs.FS, through the New controller or the Handler function.
mvc

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL