In Lua on Beans, the root route (homepage) is typically defined using a special syntax in the config/routes.lua
file. Here's how it's done:
Routes = { ["GET"] = { [""] = "welcome#index" } } --define root
This line does the following:
index
action of the welcome_controller
.This means that when a user visits the homepage of your application, the index
method in the welcome_controller
will be called to handle the request.
In Lua on Beans, routes are defined in the config/routes.lua
file. There are two main methods for defining routes: CustomRoute
and Resource
.
CustomRoute
allows you to define individual routes with specific HTTP methods and paths. Here's an example:
CustomRoute("GET", "demo/with/:id/nested/:demo/route", "welcome#ban", {
[":demo"] = "([0-9]+)" -- you can define regex per params
})
The Resource
method is used to quickly define a set of RESTful routes for a resource. It creates standard CRUD (Create, Read, Update, Delete) routes. Here's an example:
Resource("users")
This single line creates the following routes:
These routes will map to corresponding actions in the UsersController.
Lua on Beans supports nested routes, which are particularly useful for representing hierarchical relationships between resources. Here's an example of how to define nested routes:
Resource("comments", {
var_name = "comment_id", -- default value is "id"
var_regex = "([0-9a-zA-Z_\\-]+)", -- default value
parent = { "customers" }
})
This configuration creates the following nested routes:
These nested routes allow you to organize your application's endpoints in a way that reflects the relationships between different resources. In this case, comments are nested under customers, indicating that each comment belongs to a specific customer.
Lua on Beans also supports deep nested routes, allowing you to represent complex hierarchical relationships between multiple resources. Here's an example of how to define deep nested routes:
Resource("likes", {
var_name = "like_id", -- default value is "id"
var_regex = "([0-9a-zA-Z_\\-]+)", -- default value
parent = { "customers", "comments" }
})
This configuration creates the following deeply nested routes:
These deep nested routes allow you to represent complex relationships in your application. In this example, likes are nested under comments, which are in turn nested under customers. This structure indicates that each like belongs to a specific comment, which belongs to a specific customer.