Moderators: They make you lose count(Longest thread still alive!)

  • The FTB Forum is now read-only, and is here as an archive. To participate in our community discussions, please join our Discord! https://ftb.team/discord

lenscas

Over-Achiever
Jul 31, 2013
2,015
1,801
248
88 and routes can make reference to each other for as long as they are grouped together (probably also when they are not but haven't tested that yet.) The only thing they can't make a reference to are the what I call base routes defined in app.lua.
A base route looks something like: "/users/*" and are used broadly tell my program what the user want to see. If the url matches this the user wants to see a page that has to do with users. As a result my program then loads the routefile that knows all the routes for the users and thus will have routes "profile" (the /users/* part is stripped as it is already know and thus not needed any more)

If you want to make a reference to a route you need to make sure the one you want to reference too is defined first like for example
routes['awesomeRoute=function(app,helper)
--awesome code here
end
and then just
routes['someRoute]=routes['awesomeRoute']

I am probably going to make it so the functions you define in the route file have access to the routes as they currently don't have that.
 

lenscas

Over-Achiever
Jul 31, 2013
2,015
1,801
248
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
 

lenscas

Over-Achiever
Jul 31, 2013
2,015
1,801
248
92 basically it starts with 1 file which loads in other files making the program more "aware" of what needs to be done each time it loads a file
 

Someone Else 37

Forum Addict
Feb 10, 2013
1,876
1,440
168
93 It's pretty well beyond my understanding of the languages and applications involved.

In other words, I have no idea what your code does, but it sounds like it works, so that's good.