Controllers
Controllers handle requests, process data, and determine responses in your Lua on Beans application.
Render views with layouts and data
Build REST APIs with JSON responses
Access params, headers, and more
architecture Controller Structure
Controllers are Lua modules that return a table of action functions. Each action handles a specific route.
Basic Controller
app/controllers/welcome_controller.lua
local app = {}
function app.index()
Page("welcome/index", "app")
end
function app.show()
local id = Params.id
Page("welcome/show", "app", {}, { id = id })
end
return BeansEnv == "development" and HandleController(app) or app
info Key points:
welcome#index)
HandleController enables hot reload in development
view_quilt Rendering Views
Use the Page() function to render views with layouts and pass data to templates.
Page() Function
Render templates with layouts and data
-- Basic usage
Page("welcome/index", "app")
-- With template locals and globals
Page("docs/index", "docs", {}, {
title = "Documentation - Lua on Beans",
page = "index"
})
-- Full signature
Page(template, layout, locals, globals)
| Parameter | Type | Description |
|---|---|---|
template |
string | Path to view template (e.g., "welcome/index") |
layout |
string | Layout name from app/views/layouts/ |
locals |
table | Variables available only in the template |
globals |
table | Variables available in layout |
data_object JSON Responses
Build APIs by returning JSON responses with WriteJSON().
API Controller Example
Return JSON data from actions
local app = {}
function app.index()
WriteJSON({
status = "success",
users = { "alice", "bob", "charlie" }
})
end
function app.show()
local user = { id = Params.id, name = "John Doe" }
WriteJSON(user)
end
function app.redis_incr()
WriteJSON(Redis:incr("counter"))
end
return BeansEnv == "development" and HandleController(app) or app
input Request Parameters
Access URL parameters, query strings, and form data through the global Params table.
function app.show()
-- URL parameter: /users/:id
local id = Params.id
-- Query string: /search?q=hello
local query = Params.q
-- Form data from POST requests
local name = Params.name
local email = Params.email
Page("users/show", "app", {}, { id = id })
end
settings_ethernet Response Helpers
Lua on Beans provides several helper functions for crafting responses.
SetHeader("Content-Type", "application/pdf")
SetHeader("X-Custom", "value")
-- Write raw content
Write("Hello, World!")
Write(pdf:generate())
-- Auto-sets Content-Type
WriteJSON({ ok = true })
-- Load file from assets
local img = LoadAsset("image.jpg")
description Complete Example
Here's a real-world controller example from the Lua on Beans documentation site:
docs_controller.lua
Dynamic page routing with parameters
local app = {}
function app.index()
Page("docs/index", "docs", {}, {
title = "Documentation - Lua on Beans",
page = "index"
})
end
function app.show()
local page = Params.page or "index"
Page("docs/" .. page, "docs", {}, {
title = string.upper(page:sub(1,1)) .. page:sub(2) .. " - Documentation",
page = page
})
end
return BeansEnv == "development" and HandleController(app) or app
Pro Tips
Hot Reload: The HandleController(app) wrapper enables automatic reloading during developmentāno server restart needed!
Naming Convention: Controllers should be named *_controller.lua and placed in app/controllers/.