Controllers

Controllers handle requests, process data, and determine responses in your Lua on Beans application.

view_module
Page Rendering

Render views with layouts and data

data_object
JSON API

Build REST APIs with JSON responses

tune
Request Handling

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.

description

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:

check_circle Controllers are Lua tables with function actions
check_circle Action names match route definitions (e.g., welcome#index)
check_circle HandleController enables hot reload in development

view_quilt Rendering Views

Use the Page() function to render views with layouts and pass data to templates.

code

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

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
SetHeader("Content-Type", "application/pdf")
SetHeader("X-Custom", "value")
Write
-- Write raw content
Write("Hello, World!")
Write(pdf:generate())
WriteJSON
-- Auto-sets Content-Type
WriteJSON({ ok = true })
LoadAsset
-- 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:

folder_open

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
lightbulb How it works:
index Renders the docs index page with "docs" layout
show Dynamically loads pages based on URL parameter
title Capitalizes first letter for page title
lightbulb

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/.