The "C" in MVC stands for Controller, which is in many ways the most important component of the MVC triad. The controller's purpose is to act as the central switching center for incoming requests, routing data to the model and returning views back to the requestor. We've taken advantage of many of Pike's object oriented features to make working with controllers painless. The most basic of the controllers available in Fins is Fins.FinsController. It's pretty simple, and is a good starting point. There are other controllers derived from FinsController, such as XMLRPCController and DocController. You can even derive your own for special purposes. Each application has a "master controller", which is like the root of the virtual filesystem for your web application. This master controller, specified as the controller -> class entry in your application's config file, serves the root directory of the application, that is "/" and everything directly underneath it. You can mount sub-controllers from within this master controller in order to build a more complex directory structure for your application. You can also mix and match different types of controllers to achieve your desired result. The event handler method For each path component you want to respond to in your application, you need to provide an event handler function. The index function is a special case, and will be described separately. For example, if we wanted to respond to /foo, we'd need to add an event handler function called foo() to our master controller. An example event handler function for the default FinsController controller type might be:
public void foo(Fins.Request id, Fins.Response response, mixed ... args)
{
...
}
inherit Fins.FinsController;
public void index(Fins.Request id, Fins.Response response, mixed ... args)
{
response->set_data("Hello, World.");
}
inherit Fins.FinsController;
Fins.FinsController oats;
Fins.FinsController peas;
Fins.FinsController beans;
void start()
{
oats = load_controller("oats_controller");
peas = load_controller("peas_controller");
beans = load_controller("beans_controller");
}
No comments | Post a Comment| RSS Feed | BackLinks