90 ok, let me try to explain it better. (I am going to show what I want to be able to do and I think I can currently already do that)
Lapis works in that you have 1 file that get executed whenever your server gets a request, this file is called app.lua.
In this file I use the function app:match() to match functions with url's. Thus for a login page I could write app:match("users/login",function()[[code here]] end)
Now, using the "*" symbol we can match url's that don't exactly match, which is useful when you make for example a profile page. the "*" inside a match string means that everything beyond this doesn't matter for as long there is at least something . Using this we can for example easily make profile pages. Lets say we have the following app:match("users/profile/*") then the following url will work "example.com/users/profile/4" however "example.com/users/profile/" won't as it ends too early. Another key factor is that everything that is at the place of the "*" symbol in the url gets stored inside app.params.splat as a single string.
Now, lets use this feature and expand on it. Lets have a look at the folowing code I wrote:
Code:
app:get("/users/*",function(self) --note Lua doesn't have magic keywords and as a result when you need to have a function have access to the "object" it is in we need to pass it to the function, I like to call the variable that hold this object: self.
return helper.load.route(helper,"userRoutes",self)
end)
Now, helper is a special object I wrote which contains some very useful functions. One of them is the route() function. This function needs the helper object, the name of the file it needs to look for (without extension) and the app object. Currently it is called self. (If this was php I would have to pass $this instead)
This function searches inside the routes folder for a file that matches the given string. This file should once loaded return an table with more routes and functions. The route() function looks at the app.params.splat and tries to find a route that matches this. It then execute the function that belongs to it.
So, lets say we have the following url "example.com/users/profile/".
In the app.lua file we have
Code:
app:get("/users/*",function(self) --note Lua doesn't have magic keywords and as a result when you need to have a function have access to the "object" it is in we need to pass it to the function, I like to call the variable that hold this object: self.
return helper.load.route(helper,"userRoutes",self)
end)
The current url matches, the value of app.params.splat becomes "profile/" and helper.load.route(helper,"userRoutes",self) gets executed.
This loads in a file with the following table
Code:
routes['profile/*']=function(self,app,helper)[[ do stuff to load the profile here]]end
routes['profile']=function(self,app,helper) [[do stuff to get the current user id here and put it in the argument list]] self['/profile/*](self,app,helper) end
As app.params.splat currently only contains "profile" and thus it will be matched against the second function. This gets executed, fetching the users ID from somewhere and putting it in the arguments list before executing the profile rendering function.
As a result a user can now just use the url "example.com/users/profile" to get to his own profile without the need for us to redirect him or write the rendering code more then once.
Thus a summary, I can split off the routes into seperate files, which can come in handy as you end up with a lot of them.
After a route was found it can fetch data and pass that to another function that is declared in the same route file, allowing you to reuse code more easily (you can technically even load in another route file as you have access to the helper object)
Something what this example didn't show is that you can also do this
Code:
routes['profile/*']=function[[do stuff here]] end
routes['profile']=routes['profile/*']
Which basically allows you to make multiple url's match with the same route