0

I'm looking to structure my application the best way possible.

I'm adding a "Services" section to my application. This will consist of an index method, and multiple services that are supposed to be reached through /service/service1/ or /service/service2/ etc.

I'm thinking of having a ServiceController and a method for each service. Each service will then have a couple of ajax methods, like /service/service1/getdata. But with this structure the getdata won't be it's own method, but a variable (that I can serve the service1 function, but it's not what I want).

I've thought about having a "Service" Plugin, but that feels redundant if all I want to do is to add the /service/ part to the URL.

Also routes, but I couldn't come up with a good way of doing it (route /services/service1 to /service1/ for example). This way I'd also need an extra controller that would have the index method for /service/. And the controller folder would be messy with all the services, so the least thing I'd want to do is to append 'service' to the file names if it's doable.

How do I structure something like this?

tereško
  • 58,060
  • 25
  • 98
  • 150
joakimdahlstrom
  • 1,575
  • 1
  • 11
  • 23
  • Are you really calling everything "service1", "service2"...etc? If you give us the real names and some context, we might better be able to help. – Dave Aug 20 '13 at 18:24
  • No, those were just examples. They are for example "report or "api", and don't really share anything apart from the "service" category. – joakimdahlstrom Aug 20 '13 at 18:30

1 Answers1

0

Just build a normal "ServicesController" (notice the plural).

Then, if you really want, you can use routing to make "service" singular work.

In your controller, make the actions:

report() { ... }
api() { ... }
report_get_data() { ... }
api_whatever() { ... }

Or, if the functionality should be the same, just one for ajax, and one for non, then just have one action and check for whehter or not the request is ajax:

if($this->request->is('ajax') { ...
Dave
  • 28,833
  • 23
  • 113
  • 183
  • I've concidered that. What I don't like about it is that the methods that belong to one service aren't collected in one, logical place. But maybe it's the best you get? – joakimdahlstrom Aug 20 '13 at 18:47
  • How many actions are we talking? Are you going to have more than 10 or 20 per service? If not, then it's fine. Feel free to reorganize by type, but - it's hard for me to imagine having that many. If you ARE going to have a ton, then you should probably think about having these things in different controllers. Keep them in order by type like "report", "report_data", "api", "api_data"...etc can even use commenting to clearly mark where one starts and the other begins...etc – Dave Aug 20 '13 at 19:01
  • I guess around 5-10 per service, and I'll have just a few services. So it's not that it'll be too much, I'm just searching for good ways to organize things. – joakimdahlstrom Aug 20 '13 at 20:53