Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How URL routing
#1
How implement URL routing products? Where to start?

Example:
http://localhost/articles/article-1 
http://localhost/articles/article-2
Reply
#2
Hi Matija,

I asked you some questions via email, but this is a job for content handlers.

The content handler can be registered to any URL path and respond with any type of content.

Basically you would create something like this:

Code:
  myHandler := TMyContentHandler.Create;
  THandlers.Add('/articles/', '/article-1', myHandler);

and your content handler would look like this:

Code:
unit uMyContentHandler;

interface

uses
  Classes, IW.Content.Base, IWApplication, IW.HTTP.Request, IW.HTTP.Reply;

type
  TMyContentHandler = class(TContentBase)
  protected
    function Execute(aRequest: THttpRequest; aReply: THttpReply; const aPathname: string;
                    aSession: TIWApplication; aParams: TStrings): Boolean; override;
  public
    constructor Create; override;
  end;

implementation

constructor TMyContentHandler.Create;
begin
  inherited;
  mFileMustExist := False;
end;

function TMyContentHandler.Execute(aRequest: THttpRequest; aReply: THttpReply; const aPathname: string;
  aSession: TIWApplication; aParams: TStrings): Boolean;
begin
  Result := True;
  if Assigned(aReply) then begin
     aReply.WriteString('this is whatever you want to return to the caller');
  end;
end;

end.

This is a very generic content handler implementation where the response can be anything (a plain text file, a JSON or XML content, a full HTML page, an HTML fragment, JavaScript, whatever you want).


The important part here is to determine what the caller expects to receive and how it intends to use the content provided by the content handler...
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)