How URL routing

<< Click to Display Table of Contents >>

Navigation:  Forum >

How URL routing

Forum link

 


 

03-29-2023, 05:28 AM:

 

How implement URL routing products? Where to start?

 

Example:

 

http://localhost/articles/article-1 

 

http://localhost/articles/article-2

 


 

04-19-2023, 05:23 AM:

 

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

 

&nbsp;&nbsp; &nbsp;&nbsp;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...