<?xml version="1.0"?>
<atom:feed xmlns:atom="http://www.w3.org/2005/Atom" xmlns:html="http://www.w3.org/1999/xhtml">
  <atom:id>http://bill.welliver.org/atom/pike/Fins/Developer/Controller</atom:id>
  <atom:title type="text">electronic.alchemy :: Controller</atom:title>
  <atom:updated>2026-05-03T19:46:59-04:00</atom:updated>
  <atom:link href="http://bill.welliver.org/atom/pike/Fins/Developer/Controller" type="application/atom+xml"></atom:link>
  <atom:link href="http://bill.welliver.org/space/pike/Fins/Developer/Controller" type="text/html"></atom:link>
  <atom:link href="http://bill.welliver.org/rss/pike/Fins/Developer/Controller" type="application/rss+xml"></atom:link>
  <atom:generator uri="http://modules.gotpike.org/blahblah/Public.Syndication.ATOM" version="0.1">Public.Syndication.ATOM (Pike v8.0 release 702)</atom:generator>
  <atom:icon>http://bill.welliver.org/favicon.ico</atom:icon>
  <atom:logo>http://bill.welliver.org/static/images/alchemy.gif</atom:logo>
  <atom:subtitle type="xhtml"><html:div xmlns:html="http://www.w3.org/1999/xhtml"><html:p>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.</html:p><html:p class="paragraph"/>
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.<html:p class="paragraph"/>
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 -&gt; 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.<html:p class="paragraph"/>
<html:b class="bold">The event handler method</html:b><html:p class="paragraph"/>
For each path component you want to respond to in your application, you need to provide an event handler function. The <html:i class="ital">index</html:i> function is a special case, and will be described separately. For example, if we wanted to respond to <html:i class="ital">/foo</html:i>, we'd need to add an event handler function called <html:i class="ital">foo()</html:i> to our master controller. An example event handler function for the default FinsController controller type might be:<html:p class="paragraph"/>
<html:div class="code"><html:pre><html:pre>&#xD;
<html:b><html:font color="darkblue">public</html:font></html:b> <html:b><html:font color="darkgreen">void </html:font></html:b><html:b><html:font color="darkbrown">foo</html:font></html:b>(Fins.Request id, Fins.Response response, <html:b><html:font color="darkgreen">mixed </html:font></html:b><html:b><html:font color="darkbrown">...</html:font></html:b> args)&#xD;
{&#xD;
  ...&#xD;
}&#xD;
</html:pre></html:pre></html:div><html:p class="paragraph"/>
&#xD;
AS you can see, a FinsController event takes 3 arguments: an object representing the request, an object representing the response, and a set of optional arguments. The optional arguments are what you'd consider the PATH_INFO in traditional CGI scripting, and they're provided split on the slash (/) character.<html:p class="paragraph"/>
You'll note that event handlers don't return a value. The Fins.Response response object represents the response that will be returned to the client, and you'll control what that ends up being by calling methods in the response object.<html:p class="paragraph"/>
The simplest possible controller might look like this:<html:p class="paragraph"/>
<html:div class="code"><html:pre><html:pre>&#xD;
<html:b><html:font color="darkblue">inherit</html:font></html:b> Fins.FinsController;<html:p class="paragraph"/>
<html:b><html:font color="darkblue">public</html:font></html:b> <html:b><html:font color="darkgreen">void </html:font></html:b><html:b><html:font color="darkbrown">index</html:font></html:b>(Fins.Request id, Fins.Response response, <html:b><html:font color="darkgreen">mixed </html:font></html:b><html:b><html:font color="darkbrown">...</html:font></html:b> args)&#xD;
{&#xD;
  response-&gt;set_data(<html:i><html:font color="darkred">"Hello, World."</html:font></html:i>);&#xD;
}&#xD;
</html:pre></html:pre></html:div><html:p class="paragraph"/>
&#xD;
This event handler will simply return the string "Hello, World." to the client. By default, the response will carry a MIME type of <html:i class="ital">text/html</html:i>. You can use the set_type() method in the response object to change that to whatever you'd like.<html:p class="paragraph"/>
The response object provides a number of useful methods that you can use to return a not-found message, set headers and cookies, and return data from Views. Most of these are self explanatory, and you can browse the <html:a href="Fins.Response Modref" class="wiki_link_external">http://hww3.riverweb.com/dist/Fins/modref/ex/predef_3A_3A/Fins/Response.html</html:a> for details.<html:p class="paragraph"/>
<html:b class="bold">Adding Sub-controllers</html:b><html:p class="paragraph"/>
It's easy to add sub-controllers to extend your virtual filesystem and break the various tasks of your application down to more manageable and logical chunks. To do this, we create a <html:i class="ital">start() ~~method in our controller and use the ~~load_controller()</html:i> to add new sub-controllers. Note that we use this technique in order to support the ability to reload controllers when they're updated, a valuable time-saver when developing your applications.<html:p class="paragraph"/>
Consider the following sample controller:<html:p class="paragraph"/>
<html:div class="code"><html:pre><html:pre>&#xD;
<html:b><html:font color="darkblue">inherit</html:font></html:b> Fins.FinsController;<html:p class="paragraph"/>
Fins.FinsController oats;&#xD;
Fins.FinsController peas;&#xD;
Fins.FinsController beans;<html:p class="paragraph"/>
<html:b><html:font color="darkgreen">void </html:font></html:b><html:b><html:font color="darkbrown">start</html:font></html:b>()&#xD;
{&#xD;
  oats = load_controller(<html:i><html:font color="darkred">"oats_controller"</html:font></html:i>);&#xD;
  peas = load_controller(<html:i><html:font color="darkred">"peas_controller"</html:font></html:i>);&#xD;
  beans = load_controller(<html:i><html:font color="darkred">"beans_controller"</html:font></html:i>);&#xD;
}&#xD;
</html:pre></html:pre></html:div><html:p class="paragraph"/>
&#xD;
The arugment passed to <html:i class="ital">load_controller()</html:i> is a string containing the path name of the controller we want to load, relative to the application's <html:i class="ital">classes</html:i> directory.<html:p class="paragraph"/>
Each controller will be mounted as a subdirectory with the same name as the variable you assign the controller to. For example, if the class above were the master controller, we could access <html:i class="ital">oats_controller</html:i> as <html:i class="ital">/oats/</html:i>. Any subpaths will call the appropriate methods or sub-controllers just as they do for the master controller.</html:div></atom:subtitle>
</atom:feed>
