Routing

Learn how to define and organize routes in your Lua on Beans application.

home
Root Route

Define your homepage entry point

alt_route
Custom Routes

Create flexible route patterns

account_tree
Resources

RESTful CRUD routes in one line

home Root Route

In Lua on Beans, the root route (homepage) is defined in the config/routes.lua file:

Routes = { ["GET"] = { [""] = "welcome#index" } } --define root

info What this does:

check_circle Sets up a GET request for the root URL ("/")
check_circle Maps to the index action of welcome_controller

route Defining Routes

Routes are defined in the config/routes.lua file using two main methods:

alt_route

CustomRoute

Define individual routes with specific HTTP methods

CustomRoute("GET", "demo/with/:id/nested/:demo/route", "welcome#ban", {
  [":demo"] = "([0-9]+)" -- you can define regex per params
})
GET HTTP method
:id Dynamic parameters
welcome#ban Controller#action
regex Custom validation
account_tree

Resource

Generate RESTful CRUD routes with one line

Resource("users")

This single line creates all standard CRUD routes:

Method Path Action
GET /users index
GET /users/new new
POST /users create
GET /users/:id show
GET /users/:id/edit edit
PUT /users/:id update
PATCH /users/:id update
DELETE /users/:id delete

account_tree Nested Routes

Lua on Beans supports nested routes for representing hierarchical relationships between resources.

folder_special

Nested Resource Example

Comments belonging to Customers

Resource("comments", {
  var_name = "comment_id",          -- default value is "id"
  var_regex = "([0-9a-zA-Z_\\-]+)", -- default value
  parent = { "customers" }
})
arrow_forward Generated routes:
GET /customers/:customer_id/comments
GET /customers/:customer_id/comments/new
POST /customers/:customer_id/comments
GET /customers/:customer_id/comments/:comment_id
PUT /customers/:customer_id/comments/:comment_id
DELETE /customers/:customer_id/comments/:comment_id

layers Deep Nested Routes

For complex hierarchical relationships, Lua on Beans supports deep nested routes with multiple parent resources.

layers

Deep Nested Example

Likes → Comments → Customers

Resource("likes", {
  var_name = "like_id",
  var_regex = "([0-9a-zA-Z_\\-]+)",
  parent = { "customers", "comments" }
})
schema Route hierarchy:
customers chevron_right comments chevron_right likes
arrow_forward Generated routes:
GET /customers/:customer_id/comments/:comment_id/likes
POST /customers/:customer_id/comments/:comment_id/likes
GET /customers/:customer_id/comments/:comment_id/likes/:like_id
PUT /customers/:customer_id/comments/:comment_id/likes/:like_id
DELETE /customers/:customer_id/comments/:comment_id/likes/:like_id
lightbulb

Pro Tip

Use Resource for standard CRUD operations and CustomRoute for specialized endpoints. Nested routes are perfect for representing belongs-to relationships in your data model.