fcl-web

From Lazarus wiki
Revision as of 12:10, 17 July 2010 by Swen (talk | contribs) (Using multiple modules)

Using multiple modules

If there is only one module in the web application, all requests will be directed to this module.

As your web application grows, multiple modules can be used. A new module can be added by choosing 'File - New' and then one of 'Web module' or 'HTML Web Module'.

FCL-web uses the URL to determine how a HTTP request should be handled. It must therefor know which web-modules exist in the application, and for this, each module must be registered.

Each module is registered with fcl-web in the initialization section of the unit it is defined in: <delphi> RegisterHTTPModule('location',TMyModule); </delphi> The module will then be invoked if an URL is used of the form

http://www.mysite.org/mycgi.cgi/location

or

http://www.mysite.org/mycgi.cgi?module=location


Note that if multiple modules are present, the name of the module must appear in the URL, or an error will be raised.

This behaviour can also be forced for applications that have only a single module by setting the Application's property AllowDefaultModule to false: <delphi> Application.AllowDefaultModule:=False; </delphi> In that case, the fcl-web application will always require the name of the module in the URL.

The name of the request variable that determines the module name (by default, this is 'module') can be set in the Application.ModuleVariable property. The following code <delphi> Application.ModuleVariable:='m'; </delphi> ensures that the following URL is directed to TMyModule:

http://www.mysite.org/mycgi.cgi?m=location


If all this is not enough to determine the module to which the request should be passed, the Application.OnGetModule event can be used. It is of type TGetModuleEvent: <delphi> Type

 TGetModuleEvent = Procedure (Sender : TObject; ARequest : TRequest;
                              Var ModuleClass : TCustomHTTPModuleClass) of object;

</delphi> Creating an event handler for this event allows fine control over the module that is created to handle the request: the request (passed in ARequest) can be examined, and the 'ModuleClass' variable must be set to the class of the module that should handle the request.

If 'ModuleClass' is 'Nil' on return, an error will be sent to the browser.