Structuring "nested" controllers

61 views Asked by At

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?

1

There are 1 answers

3
Dave On

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') { ...