Routing
Learn how to define and organize routes in your Lua on Beans application.
Define your homepage entry point
Create flexible route patterns
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:
GET request for the root URL ("/")
index action of welcome_controller
route Defining Routes
Routes are defined in the config/routes.lua file using two main methods:
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
})
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.
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" }
})
layers Deep Nested Routes
For complex hierarchical relationships, Lua on Beans supports deep nested routes with multiple parent resources.
Deep Nested Example
Likes → Comments → Customers
Resource("likes", {
var_name = "like_id",
var_regex = "([0-9a-zA-Z_\\-]+)",
parent = { "customers", "comments" }
})
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.