delphimvcframework/samples/sessions/AppControllerU.pas
danieleteti 5601767e04 - added method TWebContext.SessionStarted (check sample Sessions to see how it works)
- added method TWebContext.SessionID
- added unit test for TWebContext.SessionStarted
2016-06-28 13:44:09 +02:00

65 lines
1.2 KiB
ObjectPascal

unit AppControllerU;
interface
uses MVCFramework,
MVCFramework.Logger,
Web.HTTPApp;
type
[MVCPath('/')]
TApp1MainController = class(TMVCController)
public
[MVCPath('/name')]
[MVCHTTPMethod([httpGET])]
procedure Index;
[MVCPath('/login/($username)')]
[MVCHTTPMethod([httpGET])]
procedure DoLogin(username: String);
[MVCPath('/logout')]
[MVCHTTPMethod([httpGET])]
procedure DoLogout;
end;
implementation
uses
Data.DBXJSON,
System.SysUtils, MVCFramework.Commons;
{ TApp1MainController }
procedure TApp1MainController.DoLogin(username: String);
begin
Session['username'] := username;
Render(204, 'No Content');
end;
procedure TApp1MainController.DoLogout;
begin
Context.SessionStop(false);
Render(204, 'No Content');
end;
procedure TApp1MainController.Index;
begin
ContentType := TMVCMediaType.TEXT_PLAIN;
// do not create session if not already created
if Context.SessionStarted then
begin
// automaticaly create the session
Render('Session[''username''] = ' + Session['username']);
end
else
begin
Render(400, 'Session not created. Do login first');
end;
end;
end.