ACF mvc.lua reference

From Alpine Linux
Revision as of 21:06, 2 November 2007 by Nangel (talk | contribs) (→‎new( self, modname): added 2 booleans to new function return value)

mvc.lua function reference

This lua module provides the basics for creating an mvc application. It is patterned loosely after the Ruby on Rails pattern - but much more simplistic.

The general pattern is to use the mvc new function to create a set of tables, and then use 'new within those tables to create sub "objects". By using metatable .__index methods, function references flow up through the parent tables. So the application is structured something like this:

ACF's MVC architecture dia source


new( self, modname)

Returns 3 values: an mvc table and 2 booleans ( true or false if the modname-controller and modname-model.lua were loaded)

The mvc table contains:


table used for comments .__index points to
conf configuration items only created if a conf table does not exist in a parent n/a
clientdata data sent from the client only created if a clientdata table does not exist in a parent n/a
worker the "controller" methods if modname is given, then modname-controller.lua module is loaded

into this table. Otherwise, an empty table is returned.

self (parent mvc object)
worker.mvc special methods run by the mvc dispatch function If the modname-controller.lua module does not initalize a .mvc table,

an empty one is created

self.mvc (parent mvc object's mvc table)
model the "model" methods if modname is given, then modname-model.lua module is loaded

into this table. Otherwise, an empty table is returned.

worker (this mvc object's worker table)

The returned table has a .__index method that points to self, so this table can inherit values from the parent table.

If the modname-controller.lua contains a .mvc.on_load function, the function is run before new returns.

The .__index metamethods mean that this code will set up inheritance as shown in the diagram:

 require("mvc")
 MVC=mvc:new()
   APP=MVC:new()
    controller=APP:new()
      subcontroller=controller:new()

ACF's inheritance dia source

If you try to run subcontroller.model.somefunction(), and it does not exist, the inheritance will look for somefunction() in ...

  1. subcontroller.worker
  2. controller
  3. controller.worker
  4. APP
  5. APP.worker
  6. MVC
  7. MVC.worker


This allows, for instance, the application to set a default method that is available to all child controllers. The reason the model looks to its parent worker table first is that controller methods are usually in the worker table, and models do not normally inherit from each other.

dispatch (self, prefix, controller, action )

The gateway for executing controller actions in a protected xpcall.

  1. Creates a self:new ( prefix .. controller) mvc object
  2. runs any existing worker.mvc.pre_exec function
  3. runs the worker.action
  4. runs any existing worker.mvc.post_exec function
  5. sends the results of the worker.action to the view_resolver
  6. and executes the view

This function does not return. If an error occurs, the exception_handler function is run.


basename ( string, suffix )

Lua implementation of basename.1 Returns string with any leading directory components removed. If specified, also remove a trailing suffix.

dirname ( string )

Lua implementation of dirname.1 Returns string with its trailing /component removed.

exception_handler ( self, message )

Prints an error message. Called if the xpcall in dispatch has an error. This is the exception_handler of last resort. The application should provide a more robust exception handler.

parse_path_info ( string )

Returns 3 strings: a prefix, controller, and action. Given a string in the format of a URI or pathspec, returns the basename as the action, the last component of the dirname as the controller, and the rest as the prefix. Missing components are returned as empty strings.

read_config ( self, modname )

Looks in various places for a modname.conf file and parses its contents into the self.conf table.


soft_require ( self, modname )

Looks for modname.lua in self.conf.appdir and returns the results of a require() If the modname does not exist, returns nil.

This function allows modules to be loaded without generating an exception if they do not exist.

soft_traceback ( self, message )

If called with no arguments, returns a debug.traceback, otherwise returns "message".


view_resolver (self )

Returns a function that prints "you do not have a view resolver". The application should provide a more robust view_resolver.