delphimvcframework/samples/sessions/AppControllerU.pas

65 lines
1.2 KiB
ObjectPascal
Raw Normal View History

2014-04-01 00:02:31 +02:00
unit AppControllerU;
interface
uses MVCFramework,
MVCFramework.Logger,
Web.HTTPApp;
type
[MVCPath('/')]
TApp1MainController = class(TMVCController)
public
2014-04-01 19:36:05 +02:00
[MVCPath('/name')]
2014-04-01 00:02:31 +02:00
[MVCHTTPMethod([httpGET])]
procedure Index;
2014-04-01 00:02:31 +02:00
[MVCPath('/login/($username)')]
[MVCHTTPMethod([httpGET])]
procedure DoLogin(username: String);
2014-04-01 00:02:31 +02:00
[MVCPath('/logout')]
[MVCHTTPMethod([httpGET])]
procedure DoLogout;
2014-04-01 00:02:31 +02:00
end;
implementation
uses
Data.DBXJSON,
System.SysUtils, MVCFramework.Commons;
2014-04-01 00:02:31 +02:00
{ TApp1MainController }
procedure TApp1MainController.DoLogin(username: String);
2014-04-01 00:02:31 +02:00
begin
Session['username'] := username;
Render(200, 'Logged in');
2014-04-01 00:02:31 +02:00
end;
procedure TApp1MainController.DoLogout;
2014-04-01 00:02:31 +02:00
begin
Context.SessionStop(false);
Render(200, 'Logged out');
2014-04-01 00:02:31 +02:00
end;
procedure TApp1MainController.Index;
2014-04-01 00:02:31 +02:00
begin
ContentType := TMVCMediaType.TEXT_PLAIN;
// do not create session if not already created
if Context.SessionStarted then
begin
// automaticaly create the session
Render('Hello ' + Session['username']);
end
else
begin
Render(400, 'Session not created. Do login first');
end;
2014-04-01 00:02:31 +02:00
end;
end.