Lua on Beans

Routing in Lua on Beans

Root Route

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:

  • It sets up a GET request for the root URL ("/").
  • It maps this request to the 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.

Defining Routes

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

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
})
      

Resource

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:

  • GET /users (index)
  • GET /users/new (new)
  • POST /users (create)
  • GET /users/:id (show)
  • GET /users/:id/edit (edit)
  • PUT /users/:id (update)
  • DELETE /users/:id (destroy)

These routes will map to corresponding actions in the UsersController.

Nested Routes

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:

  • GET /customers/:customer_id/comments
  • GET /customers/:customer_id/comments/new
  • GET /customers/:customer_id/comments/:comment_id
  • POST /customers/:customer_id/comments
  • GET /customers/:customer_id/comments/:comment_id/edit
  • PUT /customers/:customer_id/comments/:comment_id
  • DELETE /customers/:customer_id/comments/:comment_id

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.

Deep Nested Routes

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:

  • GET /customers/:customer_id/comments/:comment_id/likes
  • GET /customers/:customer_id/comments/:comment_id/likes/new
  • GET /customers/:customer_id/comments/:comment_id/likes/:like_id
  • POST /customers/:customer_id/comments/:comment_id/likes
  • GET /customers/:customer_id/comments/:comment_id/likes/:like_id/edit
  • PUT /customers/:customer_id/comments/:comment_id/likes/:like_id
  • DELETE /customers/:customer_id/comments/:comment_id/likes/:like_id

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.