Frequently Asked Questions
1.The subfolder delphistompclient (or dmustache) is empty
delphistopclient is a submodule of DelphiMVCFramework. You have to update the submodule. Using TortoiseGit the operation is trivial:
- Right click on the dmvcframework root dir
- Click on
TortiseGit->Submodule update
Using command line Git you can do the following steps
- open command line prompt
- navigate to main folder of
DelphiMVCFramework
- Run the following
git submodule update --init --recursive
Your submodules are updated, try to compile a sample to check it.
2. The ideexpert doesn't compile because look for package 'EspertsCreators'
The DelphiMVCFramework IDEExpert is still in beta. Curretly has been tested on Delphi 20 Seattle Update 1 Enterprise. If you dont have Delphi 10 Seattle Enterprise, please consider this IDEExpert as alpha/not supported. We'll be happy if you would to contribute to the development making it compatibile with Delphi 10 Seattle Professional or older version of Delphi
3. I cannot install the IDEExpert, can I use DelphiMVCFramework without it?
Yes you can, and without limitations. You have to create a new WebBroker
console project and then create an event handler for its WebModuleCreate
event like the following sample web module (chek the comments in it).
unit WebModuleUnit1;
interface
uses System.SysUtils,
System.Classes,
Web.HTTPApp,
MVCFramework; //include MVCFramework unit
type
TWebModule1 = class(TWebModule)
procedure WebModuleCreate(Sender: TObject);
private
//declare this private instance field
MVC: TMVCEngine;
public
{ Public declarations }
end;
var
WebModuleClass: TComponentClass = TWebModule1;
implementation
{$R *.dfm}
//this file will be created in the next step
uses MyControllerU;
procedure TWebModule1.WebModuleCreate(Sender: TObject);
begin
//add this 2 lines
MVC := TMVCEngine.Create(Self);
MVC.AddController(TMyController);
end;
end.
The you have to add another unit to the project, save it as MyControllerU
and copy & paste the following code.
unit MyControllerU;
interface
uses MVCFramework,
MVCFramework.Logger,
Web.HTTPApp;
type
[MVCPath('/')]
TMyController = class(TMVCController)
public
[MVCPath('/')]
[MVCHTTPMethod([httpGET])]
procedure Index(ctx: TWebContext);
[MVCPath('/hello')]
[MVCHTTPMethod([httpGET])]
procedure HelloWorld(ctx: TWebContext);
[MVCPath('/hello')]
[MVCHTTPMethod([httpPOST])]
procedure HelloWorldPost(ctx: TWebContext);
end;
implementation
uses
System.SysUtils, System.JSON;
{ If your Delphi doesn't have System.JSON use Data.DBXJSON}
procedure TApp1MainController.HelloWorld(ctx: TWebContext);
begin
Render('Hello World called with GET');
end;
procedure TApp1MainController.HelloWorldPost(ctx: TWebContext);
var
JSON: TJSONObject;
begin
JSON := ctx.Request.BodyAsJSONObject;
JSON.AddPair('modified', 'from server');
Render(JSON, false);
Log('Hello world called with POST');
end;
procedure TApp1MainController.Index(ctx: TWebContext);
begin
Redirect('/hello');
end;
end.
Run the program; your server should be ok.
When you'll understand the basics you can create more complex DMVCFramework servers. Check the samples
folder.
4. How can I download an image using TRestClient?
You have to specify the correct "Accept" Header. After that You can use the doGet method as usual:
procedure TForm1.Button1Click(Sender: TObject);
var
Clt: TRestClient;
lFs: TFileStream;
begin
Clt := TRestClient.Create('http://localhost', 8089, nil);
try
Clt.Accept('image/x-png'); //must be identical with the server side produce attribute or the content type set on the server
lFs := TFileStream.Create('downloadedlogo.png', fmCreate or fmOpenWrite);
try
lFs.CopyFrom(Clt.doGET('/static/img/logo.png', []).Body, 0);
finally
lFs.Free;
end;
finally
Clt.Free;
end;
end;