#1 2017-12-14 22:30:41

Joker
Member
Registered: 2015-07-06
Posts: 15

Multiple Controllers in an MVC Server

Hi all,
Maybe I'm missing something simple here, but this is what I am trying to do ...

I have a rather complex MVC application and I want to have a different controller (TMVCApplication) for each different area of the application i.e. I don't want ALL the code in a single unit/TMVCApplication object, as it is getting a little cumbersome.

Is there an easy way to have a single server application but have multiple controllers and/or source files split along logical lines?

Thanks in advance.

Joker.

Offline

#2 2017-12-15 08:54:10

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,659
Website

Re: Multiple Controllers in an MVC Server

There is no problem to have several TMVCApplication per TSQLRestServer instance:

      aApplication1 := TBlogApplication1.Create;
      aApplication2 := TBlogApplication2.Create;
      try
        aApplication1.Start(aServer);
        aApplication2.Start(aServer);
        aHTTPServer := TSQLHttpServer.Create('8092',aServer
          {$ifndef ONLYUSEHTTPSOCKET},'+',useHttpApiRegisteringURI{$endif});
        try
          aHTTPServer.RootRedirectToURI('blog/default'); // redirect / to blog/default
          aServer.RootRedirectGet := 'blog/default';  // redirect blog to blog/default
          writeln('"MVC Blog Server" launched on port 8092 using ',aHttpServer.HttpServer.ClassName);
     ...

(starting from sample 30)

Each controller should have its own dedicated interface type, for sure.

Offline

#3 2017-12-15 08:59:35

Chaa
Member
Registered: 2011-03-26
Posts: 249

Re: Multiple Controllers in an MVC Server

You can create any number of TMVCApplication with different URI for each.

procedure TBlogApplication.Start(aServer: TSQLRestServer);
begin
...
  // publish IBlogApplication using Mustache Views (TMVCRunOnRestServer default)
  fMainRunner := TMVCRunOnRestServer.Create(Self, nil, 'blog1' { <- aSubURI } );
...
end;

Offline

#4 2017-12-15 09:04:56

Chaa
Member
Registered: 2011-03-26
Posts: 249

Re: Multiple Controllers in an MVC Server

ab wrote:

Each controller should have its own dedicated interface type, for sure.

Not interface type, but method names if you use aSubURI = '', or different aSubURI for each TMVCApplication.

Offline

#5 2017-12-15 11:09:52

Joker
Member
Registered: 2015-07-06
Posts: 15

Re: Multiple Controllers in an MVC Server

Excellent. Thanks guys.
It was easier than I thought.

Offline

#6 2022-05-13 07:54:59

scutlemon
Member
Registered: 2022-04-18
Posts: 13

Re: Multiple Controllers in an MVC Server

I also encountered similar problems. After referring to this post, I still don't know how to solve them. I have some controllers in one application. How do I use different methods?
For example ,  I have a sqlite database , "/user/add" assign to procedure user_add , "/order/create" assign to procedure order_create.
In my code , TSQLHttpServer use to http service  , TSQLRestServerDB use to database process . How can TSQLRestServerDB set to multi path to procedure ? Or any other code support it ?

Offline

#7 2022-05-13 08:30:29

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,659
Website

Re: Multiple Controllers in an MVC Server

This post is about MVC web applications, not regular REST services as you try to implement, if I understand correctly.

Offline

#8 2022-05-13 09:16:37

scutlemon
Member
Registered: 2022-04-18
Posts: 13

Re: Multiple Controllers in an MVC Server

ab wrote:

This post is about MVC web applications, not regular REST services as you try to implement, if I understand correctly.

Maybe I misunderstood this post. But how should I realize my demand?
And thank you for your reply.

Offline

Board footer

Powered by FluxBB